我目前正在创建一个网络爬虫,以便为一个学校项目从网站收集数据。这个问题是,我得到了以下错误代码(仅来自这一个网页):
<h1>You are viewing this page in an unauthorized frame window.</h1>
0
[Finished in 5.4s]下面是完整的代码:
#Creating my own webcrawler
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup
import urllib.request
myurl = 'https://nvd.nist.gov/vuln/data-feeds'
myReq = (myurl)
req = urllib.request.Request(
myurl,
data=None,
headers={
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36'
}
)
#opening my connection, grabbing the page
uClient = uReq(myurl)
page_html = uClient.read()
uClient.close()
#html parsing
page_soup = soup(page_html, 'html.parser')
print(page_soup.h1)
containers = page_soup.findAll('td rowspan="1"',{'class':'x-hidden-focus'})
print(len(containers))如你所见,我甚至添加了一个用户代理,但我仍然收到这个错误消息。如有任何帮助,我们不胜感激!
发布于 2019-03-19 18:19:32
我相信'findAll‘方法的第一个参数对您没有帮助,所以这个问题可能与HTTP请求-响应周期无关。
我查询了您正在使用的url,文档中所有'td‘元素的所有可能属性是:
{'class': ['xml-file-size', 'file-20']}
{'class': ['xml-file-type', 'file-20']}
{'colspan': '2', 'class': ['xml-file-type', 'file-20']}
{'rowspan': '3'}
{'colspan': '2'}
{}这使得查询'rowspan‘为1和'class’‘x-hidden focus’返回空列表。
尝试倒数第二行:
containers = page_soup.findAll('td', {'colspan'='1', 'class':'file-20'})或者:
containers = page_soup.findAll('td', {'rowspan': '3'})或者仅仅是:
containers = page_soup.findAll('td')由你决定你要找的具体的'td‘元素。
也可以查看documentation,了解使用BeautifulSoup的更多方法,包括将函数作为参数传递等。
https://stackoverflow.com/questions/55238005
复制相似问题