用编程给你的英语学习加点料中我写了几个可能在英语学习中用到的脚本,但其实在脚本的最开始我调用了一个外部程序,叫ydict,爬取的是有道词典。
这几天刚好在学习爬虫,仿照着也实现了一个类似的极简的脚本,不到20行。当然缺陷还很多。
import requests
import sys
from bs4 import BeautifulSoup
def getHTMLText(url):
try:
hd={"user-agent":"chrome/10"}
r=requests.get(url,headers=hd)
r.raise_for_status()
r.encoding=r.apparent_encoding
soup=BeautifulSoup(r.text,"html.parser")
return soup.strong.string
except:
return "爬取失败"
if __name__ == "__main__":
word=sys.argv[1]
#跟百度关键词搜索差不多;首先需要分析接口
url="http://m.dict.cn/msearch.php?q="+word
print(getHTMLText(url))
使用
PS D:\02Work\pachong> python .\shizhan6.py neoantigen
[生]新抗原
网友评论