我正在做一个网站抓取122个不同的网页,每页10个条目。每次运行代码时,代码都会在随机页面和随机条目上中断。我可以在一个url上运行代码一次,它工作,而其他时候它没有。在
def get_soup(url):
soup = BeautifulSoup(requests.get(url).content, 'html.parser')
return soup
def from_soup(soup, myCellsList):
cellsList = soup.find_all('li', {'class' : 'product clearfix'})
for i in range (len(cellsList)):
ottdDict = {}
ottdDict['Name'] = cellsList[i].h3.text.strip()
这只是我的一段代码,但这正是发生错误的地方。问题是,当我使用这个代码时,h3标记并不总是出现在cellsList的每个项目中。这将导致在运行最后一行代码时出现非类型错误。然而,当我检查网页时,h3标记总是存在于HTML中。在
same comparison made from subsequent soup request
是什么导致了这些差异?我如何才能避免这个问题?我能够成功地运行代码一段时间,它似乎突然停止工作。该代码能够毫无问题地抓取一些页面,但它不会在随机页面的随机条目上随机注册h3标记。在
在浏览站点页面时,不同元素的html中有细微的差异,获取名称的最佳方法实际上是选择外部div并从锚中提取文本。在
这将获得每个产品的所有信息,并将其放入dicts中,其中的键是“组织”、“细胞”等。。这些值是相关的描述:
运行后:
^{pr2}$您将看到1211个包含所有数据的dicts。在
相关问题 更多 >
编程相关推荐