二维码
微世推网

扫一扫关注

当前位置: 首页 » 快闻头条 » 科技资讯 » 正文

Python爬虫之Selector选择器实战之xpa

放大字体  缩小字体 发布日期:2023-03-09 12:42:59    作者:郭欣月    浏览次数:189
导读

20230304星期六:爬虫之Selector选择器实战之xpath/css提取数据:# 方式一:# Selector选择器,xpath提取数据:all_trs = select.xpath('/*;q=0.8,application/signed-exchange;v=b3;q=0.9"}response = requ

20230304星期六:

爬虫之Selector选择器实战之xpath/css提取数据:

# 方式一:# Selector选择器,xpath提取数据:all_trs = select.xpath('/*;q=0.8,application/signed-exchange;v=b3;q=0.9"}response = requests.get(url,headers=headers)response.encoding = 'gb2312'# print(response.text)select = Selector(response.text)# 方式一:# Selector选择器,xpath提取数据:# all_trs = select.xpath('//*[等id="list"]/table/tbody/tr').getall()# 方式二:# Selector选择器,css提取数据:# #list > table > tbody# #list > table > tbody > tr:nth-child(1) > td:nth-child(1)all_trs = select.css('#list>table>tbody>tr').getall()print('all_trs',type(all_trs),len(all_trs),all_trs)# 提取数据:ip_list = []import refor li in all_trs: ip_img = re.findall("<td>(.*?)</td>",li) # print('=====') # print(type(ip_img),ip_img) ip = ip_img[0] port = ip_img[1] proxy_type = ip_img[3] ip_list.append(proxy_type+'\t'+ip+':'+port)print('ip_list:',ip_list)import datetimefilepath = './ip代理池_' + datetime.datetime.strftime(datetime.datetime.today(),"%Y%m%d_%H%M%S") + '.txt'print('filepath:',filepath)# 保存到文件:for ip in ip_list: with open(filepath,'a+',encoding='utf-8') as f: f.write(ip+'\n')

 
(文/郭欣月)
打赏
免责声明
• 
本文为郭欣月原创作品•作者: 郭欣月。欢迎转载,转载请注明原文出处:http://www.udxd.com/news/show-372827.html 。本文仅代表作者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,作者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们邮件:weilaitui@qq.com。
 

Copyright©2015-2023 粤公网安备 44030702000869号

粤ICP备16078936号

微信

关注
微信

微信二维码

WAP二维码

客服

联系
客服

联系客服:

24在线QQ: 770665880

客服电话: 020-82301567

E_mail邮箱: weilaitui@qq.com

微信公众号: weishitui

韩瑞 小英 张泽

工作时间:

周一至周五: 08:00 - 24:00

反馈

用户
反馈