繁体   English   中英

Python BeautifulSoup无法读取div标签

[英]Python BeautifulSoup can't read div tag

我正在尝试从这个页面获取我正在处理的项目的产品: lazadapage ispection使用:

from bs4 import BeautifulSoup
import urllib
import re
r = urllib.urlopen("http://www.lazada.co.id/catalog/?q=note+2").read()
soup = BeautifulSoup(r,"lxml")
letters = soup.findAll("span",class_=re.compile("product-card__name"))
print type(letters) 
print letters[0]

当我这样做时,我收到以下错误:

Traceback (most recent call last):
  File "C:/Python27/project/testaja.py", line 9, in 
    print letters[0]
IndexError: list index out of range

有什么想法吗?

我想你可能已经过多地浏览了他们的页面,在浏览器中导航并查看网页返回的内容。

此外,您可以修改代码,以便检查页面响应标头,以确保在尝试抓取页面之前正确返回页面。 我修改了您的代码以显示以下示例:

from bs4 import BeautifulSoup
import urllib
import re

r = urllib.urlopen("http://www.lazada.co.id/catalog/?q=note+2")
header_code = r.getcode()

if header_code == 200:
    html = r.read()
    soup = BeautifulSoup(html, "lxml")
    letters = soup.findAll("span", {"class" : re.compile("product-card__name")})

    for letter in letters:
        print letter
else:
    print("oops, something went wonky. Page response was: %s"% header_code)

Python beautifulsoup find_all 找不到<div class="“" ”></div><div id="text_translate"><p>我正在尝试使用 beautifulsoup 来查找 HTML 标签中的内容。 但是当标签是/ <strong>div class=" "</strong> /时,就不行了。 双引号中有<strong>空格</strong>时不能正确识别。</p><p> 这是我的代码:</p><pre> from bs4 import BeautifulSoup if __name__ == "__main__": soup = BeautifulSoup(open("1946.html", encoding='utf-8'), 'lxml') for k in (soup.find_all('div', class_=" ")): print(k)</pre><p> 谢谢你的帮助。</p></div>

[英]Python beautifulsoup find_all can‘t find <div class=“ ”>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM