055动动手第一题的代码
本帖最后由 AINIDEREN 于 2020-2-27 19:55 编辑为啥小甲鱼的代码在我的电脑上会报错,是时间太久了吗?还是我的电脑的问题?
感觉爬虫好难啊?好多模块都没有学到
import urllib.request
import urllib.parse
import re
from bs4 import BeautifulSoup
def main():
keyword = input("请输入关键词:")
keyword = urllib.parse.urlencode({"word":keyword})
response = urllib.request.urlopen("http://baike.baidu.com/search/word?%s" % keyword)
html = response.read()
soup = BeautifulSoup(html, "html.parser")
for each in soup.find_all(href=re.compile("view")):
content = ''.join()
url2 = ''.join(["http://baike.baidu.com", each["href"]])
response2 = urllib.request.urlopen(url2)
html2 = response2.read()
soup2 = BeautifulSoup(html2, "html.parser")
if soup2.h2:
content = ''.join()
content = ''.join()
print(content)
if __name__ == "__main__":
main()
for each in soup.find_all(href=re.compile("view")):
#改成
for each in soup.find_all(href=re.compile("viewPageContent")):
#就好了
沉好人 发表于 2020-2-28 10:48
能爬取,但是获取的主标题不正确。
页:
[1]