python BeautifulSoup的使用出错
__author__ = 'zy'
import re
import urllib
from BeautifulSoup import *
def get_content(url):
""".doc"""
html=urllib.urlopen(url)
content=html.read()
html.close()
return content
def get_images(info):
soup=BeautifulSoup(info)
all_img=soup.find_all('img',class_="BDE_Image")
x=1
for img in all_img:
image_name='%s.jpg' % x
urllib.urlretrieve(img['src'],image_name)
print img['src']
x+=1
info=get_content("https://tieba.baidu.com/p/4803144798?red_tag=1205304620")
print get_images(info)
输出执行后
Traceback (most recent call last):
File "C:\Users\zy\Desktop\py\soup.py", line 29, in <module>
print get_images(info)
File "C:\Users\zy\Desktop\py\soup.py", line 17, in get_images
all_img=soup.find_all('img',class_="BDE_Image")
TypeError: 'NoneType' object is not callable
import re
import urllib
from BeautifulSoup import *
def get_content(url):
""".doc"""
html=urllib.urlopen(url)
content=html.read()
html.close()
return content
def get_images(info):
soup=BeautifulSoup(info)
all_img=soup.find_all('img',class_="BDE_Image")
x=1
for img in all_img:
image_name='%s.jpg' % x
urllib.urlretrieve(img['src'],image_name)
print img['src']
x+=1
info=get_content("https://tieba.baidu.com/p/4803144798?red_tag=1205304620")
print get_images(info)
输出执行后
Traceback (most recent call last):
File "C:\Users\zy\Desktop\py\soup.py", line 29, in <module>
print get_images(info)
File "C:\Users\zy\Desktop\py\soup.py", line 17, in get_images
all_img=soup.find_all('img',class_="BDE_Image")
TypeError: 'NoneType' object is not callable
...全文
相关推荐
用beautifulsoup爬页面
正则表达式虽然强大,但在处理复杂的HTML结构时往往需要编写复杂的模式,而BeautifulSoup允许我们通过元素的属性、标签名、文本内容等来定位元素,这使得代码更易读、更不易出错。
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
在使用Python的BeautifulSoup库解析HTML或XML文档时,可能会遇到`TypeError: list indices must be integers or slices, not str
Python代码
本项目提供了使用Python进行网页抓取的一个实例,这将帮助你理解和应用相关技术。以下是对这个Python代码项目的详细解析。首先,我们需要了解Python中用于网页抓取的库。
Python爬虫基础知识详解.zip
正则表达式灵活但易出错,DOM解析(如BeautifulSoup)更适用于结构化的HTML,CSS选择器则可以快速定位元素,与前端开发紧密相关。5.
Python爬虫运用正则表达式的方法和优缺点
在实际的爬虫项目中,除了使用正则表达式之外,还可以使用其他工具,如BeautifulSoup、lxml等。


