Python爬取商家联系电话以及各种数据的方法
作者:well2049 时间:2023-07-24 18:39:38
上次学会了爬取图片,这次就想着试试爬取商家的联系电话,当然,这里纯属个人技术学习,爬取过后及时删除,不得用于其它违法用途,一切后果自负。
首先我学习时用的是114黄页数据。
下面四个是用到的模块,前面2个需要安装一下,后面2个是python自带的。
import requests
from bs4 import BeautifulSoup
import csv
import time
然后,写个函数获取到页面种想要的数据,记得最后的return返回一下,因为下面的函数要到把数据写到csv里面。
def get_content(url,data=None):
header = {
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Encoding': 'gzip, deflate',
'Accept-Language': 'zh-CN,zh;q=0.8',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.104 Safari/537.36',
}
r = requests.get(url, headers=header)
soup = BeautifulSoup(r.content, 'html.parser')
data = soup.body.find('div',{'id':'news_con'})
ul = data.find('ul')
lis = ul.find_all('li')
pthons=[]
for item in lis:
rows=[]
name= item.find('h4').string
rows.append(name)
tel = item.find_all("div")[2].string
rows.append(tel)
pthons.append(rows)
time.sleep(1)
return pthons
接着:把数据写到表格里面。我这里用到的是csv,方便阅览。
def write_data(data,name):
file_name=name
with open(file_name, "w", newline='') as csvfile:
writer = csv.writer(csvfile)
writer.writerow(["商铺名称", "联系电话"])
writer.writerows(data)
print('抓取完成')
最后就是执行这些函数:
if __name__ == '__main__':
url = 'http://ty.114chn.com/CustomerInfo/Customers?cid=008004008&page=2'
mydata = get_content(url)
write_data(mydata,'phone.csv')
在这里我想到应该把url写成动态的,因为这里面有页数。让page写成循环自动+1,当然,可以在网页看到一共多少页。写个循环执行。就更完美了。
来源:https://blog.csdn.net/well2049/article/details/79115115
标签:Python,爬取,电话
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
JS控制输入框内字符串长度
2024-02-24 19:30:59
完美的js验证网址url(正则表达式)
2008-06-07 09:36:00
Antd的table组件表格的序号自增操作
2024-04-28 09:31:40
![](https://img.aspxhome.com/file/2023/7/132897_0s.jpg)
Python实现注册、登录小程序功能
2022-12-03 17:50:11
![](https://img.aspxhome.com/file/2023/6/68326_0s.png)
Python2.x与3.x版本有哪些区别
2023-11-01 08:34:31
Java操作MongoDB数据库示例分享
2023-06-30 11:25:03
SQL学习笔记七函数 数字,日期,类型转换,空值处理,case
2011-09-30 11:48:25
Python wxPython库使用wx.ListBox创建列表框示例
2021-12-06 21:10:56
![](https://img.aspxhome.com/file/2023/5/95745_0s.png)
Python及Django框架生成二维码的方法分析
2023-10-11 22:25:48
Pandas 类型转换astype()的实现
2022-03-13 17:39:52
![](https://img.aspxhome.com/file/2023/1/64331_0s.jpg)
10个Python面试常问的问题(小结)
2023-04-11 19:36:15
python字典嵌套字典的情况下找到某个key的value详解
2021-12-07 12:04:37
Git实现克隆历史的某个版本
2023-03-24 01:39:54
![](https://img.aspxhome.com/file/2023/6/132406_0s.png)
python3正则提取字符串里的中文实例
2022-07-01 18:47:45
![](https://img.aspxhome.com/file/2023/9/134789_0s.jpg)
Python中全局变量和局部变量的理解与区别
2022-11-13 15:35:14
![](https://img.aspxhome.com/file/2023/5/104995_0s.png)
numpy 进行数组拼接,分别在行和列上合并的实例
2021-06-28 00:06:40
![](https://img.aspxhome.com/file/2023/0/123560_0s.jpg)
六行python代码的爱心曲线详解
2022-04-09 23:15:17
![](https://img.aspxhome.com/file/2023/3/114663_0s.png)
MySQL多实例安装开机自启动服务配置过程
2024-01-14 14:32:17
matplotlib图例legend语法及设置的方法
2023-01-07 04:23:37
![](https://img.aspxhome.com/file/2023/6/78516_0s.png)
vue.js实现简易折叠面板
2024-05-08 09:33:47
![](https://img.aspxhome.com/file/2023/1/130271_0s.jpg)