爬虫学习2--使用requests、bs4以及csv爬取保存网页信息

文章所有知识基本来自个人的学习整理

目录

准备工作

代码

结果展示


准备工作

准备好我们需要的库以及我们要爬取的网页url等基本信息

pip install requests
pip install bs4

代码

本次演示我们选择了山东省菜价,来源于网络

读取多个页面数据时,可能会出现下面

AttributeError: 'NoneType' object has no attribute 'find_all' 的错误,我查阅了别人所说的解决办法,加了请求头headers之后还会出现的话,有可能是因为访问网站的时候出现了下面的验证情况

import requests
from bs4 import BeautifulSoup
import csv
f=open('菜价.csv',mode='w')
csvwriter=csv.writer(f)
#通过观察url我们可以发现,翻页时url的变化与页码相符合,因此通过for循环可以同时爬取多页数据
for i in range(3):
	url='https://www.cnhnb.com/hangqing/cdlist-2003192-0-0-0-0-'+str(i)+'/'
	r=requests.get(url,verify=False,headers={'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36'})
	r.encoding='utf-8'
	tt=r.text#获取需要的源代码
	page=BeautifulSoup(tt,'html.parser')
	content=page.find('div',attrs={'class':"quotation-content"})
	samples=content.find_all('li',attrs={'class':"market-list-item"})#使用attrs,避免使用class产生关键字冲突
	for sample in samples:
		s=sample.find_all('span')
		date=s[0].text#这是html中的内容,用于获取标签里面的内容
		product=s[1].text
		place=s[2].text
		price=s[3].text
		keep=s[4].text
		csvwriter.writerow([date,product,place,price,keep])
f.close()
print('over!!')

r.close()#最后关闭请求

结果展示

 

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以的,以下是一个简单的Python爬虫程序,可以英雄联盟2020年LPL比赛的赛事及人员数据,并将数据存储为CSV格式: ```python import requests import csv from bs4 import BeautifulSoup # 设置请求头 headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'} # 获比赛数据 def get_match_data(): # 设置URL url = 'https://lol.qq.com/sport/index.shtml' # 发送请求 response = requests.get(url, headers=headers) # 解析HTML soup = BeautifulSoup(response.content, 'html.parser') # 获比赛列表 match_list = soup.find_all('div', {'class': 'match_list'}) # 遍历比赛列表 for match in match_list: # 获比赛名称 match_name = match.find('div', {'class': 'title'}).text # 获比赛日期 match_date = match.find('div', {'class': 'date'}).text # 获比赛人员列表 match_person_list = match.find_all('div', {'class': 'person'}) # 遍历比赛人员列表 for person in match_person_list: # 获人员名称 person_name = person.find('div', {'class': 'name'}).text # 获人员位置 person_position = person.find('div', {'class': 'pos'}).text # 获人员战绩 person_score = person.find('div', {'class': 'score'}).text # 将数据写入CSV文件 with open('lol_match_data.csv', mode='a+', encoding='utf-8', newline='') as file: writer = csv.writer(file) writer.writerow([match_name, match_date, person_name, person_position, person_score]) if __name__ == '__main__': # 写入CSV文件表头 with open('lol_match_data.csv', mode='w', encoding='utf-8', newline='') as file: writer = csv.writer(file) writer.writerow(['比赛名称', '比赛日期', '人员名称', '人员位置', '人员战绩']) # 获比赛数据 get_match_data() ``` 在代码中,我们使用requests模块发送HTTP请求,然后使用BeautifulSoup模块解析HTML,从而获比赛和人员数据。最后,我们使用csv模块将数据存储到CSV文件中。 请注意,此程序仅供学习参考,并且需要遵守相关网站的数据使用规定。如果您想进行商业用途,请先咨询相关部门并遵守相关法律法规。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱打羽毛球的小怪兽

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值