|
|
发表于 2019-8-23 11:17:03
|
显示全部楼层
- import urllib.request
- import urllib.parse
- import re
- from bs4 import BeautifulSoup
- def main():
- keward = input("请输入关键字:")
- keward = urllib.parse.urlencode({"word":keward})
- response = urllib.request.urlopen("http://baike.baidu.com/search/word?%s" %keward)
- html = response.read()
- soup = BeautifulSoup(html, "html.parser")
- for each in soup.find_all(href = re.compile("view")):
- content = ''.join([each.text])
- tmp = each["href"].split('=')
- if len(tmp) == 2:
- each["href"] = ''.join([tmp[0], urllib.parse.urlencode({'':tmp[1]})])
- url2 = ''.join(["http://baike.baidu.com", each["href"]])
- response2 = urllib.request.urlopen(url=url2)
- html2 = response2.read().decode("utf-8")
- soup2 = BeautifulSoup(html2, "html.parser")
-
- if soup2.h2:
- content = ''.join([content, soup2.h2.text])
- content = ''.join([content, "->", url2])
- print(content)
- if __name__ == "__main__":
- main()
复制代码
each["href"]这个里面有可能有中文,你需要转码 |
|