|
楼主 |
发表于 2020-6-3 16:27:17
|
显示全部楼层
import urllib.request
import urllib.parse
import re
from bs4 import BeautifulSoup
def main():
keyword = input("请输入关键词:")
keyword = urllib.parse.urlencode({"word":keyword})
url="http://baike.baidu.com/search/word?%s" % keyword
req=urllib.request.Request(url)
req.add_header('Use-Agent','Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3314.0 Safari/537.36 SE 2.X MetaSr 1.0')
response=urllib.request.urlopen(req)
html = response.read()
soup = BeautifulSoup(html, "html.parser")
for each in soup.find_all(href=re.compile("item"))[7:]:
content = ''.join([each.text])
url2 = ''.join(["http://baike.baidu.com", each["href"]])
response2 = urllib.request.urlopen(url2)
html2 = response2.read().decode()
soup2 = BeautifulSoup(html2, "html.parser")
if soup2.h3:
content = ''.join([content, soup2.h2.text])
content = ''.join([content, " -> ", url2])
print(content)
if __name__ == "__main__":
main()
|
|