鱼C论坛

 找回密码
 立即注册
查看: 941|回复: 3

[已解决]爬百度词条出现问题

[复制链接]
发表于 2019-2-22 13:35:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
这是代码:
  1. import urllib.parse
  2. import urllib.request
  3. import re
  4. from bs4 import BeautifulSoup

  5. def main():
  6.     keyword = input ("请输入关键字:")
  7.     keyword = urllib.parse.urlencode({"word":keyword})
  8.    
  9.     response =\
  10. urllib.request.urlopen("http://baike.baidu.com/sarch/word? % s" % \
  11. keyword)

  12.     html = response.read()
  13.     soup = BeautifulSoup(html,"html.parser")

  14.     for each in soup.find_all(href = re.compile("view")):
  15.         content = ''.join([each.text])
  16.         url2 = ''.join(["http://baike.baidu.com",each["href"]])
  17.         response2 = urllib.request.urlopen(url2)
  18.         html2 = response2.read()
  19.         soup2 = BeautifulSoup(html2,"html.parse")
  20.         if soup2.h2:
  21.             content = ''.join([content,soup2.h2.text])
  22.         content = ''.join([content,"->",url2])
  23.         print(content)

  24. if __name__ == "__main__":
  25.     main()
复制代码

错误信息:

  Message=HTTP Error 400: Bad Request
  Source=
  StackTrace:
  File "D:\Python\PythonApplication22\PythonApplication22\PythonApplication22.py", line 12, in main
    keyword)
  File "D:\Python\PythonApplication22\PythonApplication22\PythonApplication22.py", line 29, in <module>
    main()



表示满脸懵
最佳答案
2019-2-22 13:50:31
url的%s那里你加多余的空格干嘛。改成这样就行了:
  1. def main():
  2.     keyword = input ("请输入关键字:")
  3.     keyword = urllib.parse.urlencode({"word":keyword})
  4.     response = urllib.request.urlopen("http://baike.baidu.com/sarch/word?%s" % keyword)
  5.     html = response.read()
  6.     print(html)
  7.     soup = BeautifulSoup(html,"html.parser")
  8.     for each in soup.find_all(href = re.compile("view")):
  9.         content = ''.join([each.text])
  10.         url2 = ''.join(["http://baike.baidu.com",each["href"]])
  11.         response2 = urllib.request.urlopen(url2)
  12.         html2 = response2.read()
  13.         soup2 = BeautifulSoup(html2,"html.parse")
  14.         if soup2.h2:
  15.             content = ''.join([content,soup2.h2.text])
  16.         content = ''.join([content,"->",url2])
  17.         print(content)


  18. if __name__ == "__main__":
  19.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2019-2-22 13:48:36 | 显示全部楼层
你先确定链接是否可用。

我看现在的链接是这样的:
https://baike.baidu.com/search/none?word=测式&pn=0&rn=10&enc=utf8
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2019-2-22 13:50:31 | 显示全部楼层    本楼为最佳答案   
url的%s那里你加多余的空格干嘛。改成这样就行了:
  1. def main():
  2.     keyword = input ("请输入关键字:")
  3.     keyword = urllib.parse.urlencode({"word":keyword})
  4.     response = urllib.request.urlopen("http://baike.baidu.com/sarch/word?%s" % keyword)
  5.     html = response.read()
  6.     print(html)
  7.     soup = BeautifulSoup(html,"html.parser")
  8.     for each in soup.find_all(href = re.compile("view")):
  9.         content = ''.join([each.text])
  10.         url2 = ''.join(["http://baike.baidu.com",each["href"]])
  11.         response2 = urllib.request.urlopen(url2)
  12.         html2 = response2.read()
  13.         soup2 = BeautifulSoup(html2,"html.parse")
  14.         if soup2.h2:
  15.             content = ''.join([content,soup2.h2.text])
  16.         content = ''.join([content,"->",url2])
  17.         print(content)


  18. if __name__ == "__main__":
  19.     main()
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2019-2-22 16:10:01 | 显示全部楼层
Charles未晞 发表于 2019-2-22 13:50
url的%s那里你加多余的空格干嘛。改成这样就行了:

手抖了……
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 04:41

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表