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?{0}".format(keyword)
# print('[url] --> {0}'.format(url))
response = urllib.request.urlopen(url)
html = response.read()
soup = BeautifulSoup(html, "html.parser")
for each in soup.find_all(href=re.compile("view")):
content = ''.join([each.text])
root_url = "http://baike.baidu.com"
href_url = each["href"]
url2 = urllib.parse.urljoin(root_url, href_url)
# print('[url2] --> {0}'.format(url2))
url2 = urllib.parse.quote(url2, safe=':/?#&')
response2 = urllib.request.urlopen(url2)
html2 = response2.read()
soup2 = BeautifulSoup(html2, "html.parser")
if soup2.h2:
content = ''.join([content, soup2.h2.text])
decode_url = urllib.parse.unquote(url2)
decode_url = urllib.parse.unquote(decode_url)
content = ''.join([content, " -> ", decode_url])
print(content)
if __name__ == "__main__":
main()
|