|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
有如下一段代码:(摘自小甲鱼搜索猪八戒词条,由于没有发布带url权限的帖子,链接用空格隔开了了 请忽略链接导致的错误)
import urllib.request
import urllib.parse
import re
from bs4 import BeautifulSoup
def main():
keyword = input("请输入关键字:")
keyword = urllib.parse.urlencode({"htt p s:// bai ke. bai du.c om /se arch /wor d?%sword":keyword})
resp = urllib.request.urlopen('' % keyword)
html = resp.read()
soup = BeautifulSoup(html,"html.parse")
for each in soup.find_asll(href=re.compile('view')):
context = ''.join([each.text])
url2 = ''.join(['htt p://bai ke.ba idu .co m',each['href']])
resp2 = urllib.request.urlopen(url2)
html2 = resp2.read()
soup2 = BeautifulSoup(html2,'html2.parse')
if soup2.h2:
context = ''.join(content,soup2.h2.text)
content =''.join([content,'->',url2])
if __name__ == '__main__':
main()
有如下两个问题:
1) html = resp.read() , read()的作用是什么?
2). 当执行程序的时候, 报如下错误:(是什么原因呢?)
>>>请输入关键字:猪八戒
Traceback (most recent call last):
File "D:\program\python_code\search_pig.py", line 25, in <module>
main()
File "D:\program\python_code\search_pig.py", line 12, in main
soup = BeautifulSoup(html,"html.parse")
File "D:\program\Python\Python312\Lib\site-packages\bs4\__init__.py", line 250, in __init__
raise FeatureNotFound(
bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html.parse. Do you need to install a parser library?
1)html = resp.read() , read()的作用是什么?
`read()` 的作用是读取从服务器返回的整个页面内容,返回类型是字节流。这对于进一步处理和解析页面很有用。
2) 当执行程序的时候,报如下错误:bs4.FeatureNotFound: Couldn't find a tree builder with the features you requested: html.parse.
这个错误的原因是你在使用 BeautifulSoup 时指定的解析器 `"html.parse"` 是不正确的。正确的应该是 `"html.parser"`。此外,你可能还想检查代码的其他地方,比如 `soup.find_asll` 应该修正为 `soup.find_all`,同时注意拼写和网址链接中的多余空格。
以下是修正后的关键部分代码:
- soup = BeautifulSoup(html, "html.parser")
- ...
- for each in soup.find_all(href=re.compile('view')):
- url2 = ''.join(['http://baike.baidu.com', each['href']])
- ...
- soup2 = BeautifulSoup(html2, 'html.parser')
- ...
复制代码
请确保你的 Python 环境中安装了 `beautifulsoup4` 模块,并且在 URL 和字符串处理上保持正确的格式。
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
|
|