对爬取的网页源代码进行分析,定位
try:
有可能引发异常的语句
except 引发异常的类型:
处理异常的语句
else:
依赖try语句执行成功的语句
import requests
from bs4 import BeautiflSoup
response = requests.get("http://www.baidu.com")
soup = BeautifulSoup(response.text,"html.parser")
books = soup.find_all('a',class_='pic')
prices = soup.find_all('span',class_='search_now_price')
for price in prices:
print(price.string)
for book in books:
print(boo.attrs['title'])
#print(response.text)
#print(respnse.content)
#print(type(response.text))
#print(response.url)
#print(response.encoding)
接下去学习BeautifulSoup库的一些用法,重点先看soup.find_all()。
接下去吧数据存到文件中。
网友评论