栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

数据解析(re,bs,xpath)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据解析(re,bs,xpath)

XPATH:

# import requests
# from lxml import etree,html
#
# url='http://wjw.gz.gov.cn/ztzl/xxfyyqfk/yqtb/index.html'
# headers={
#     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.12151 SLBChan/25'
# }
# response=requests.get(url,headers=headers).text
# # print(response)
# response=response.replace('m','')
# doc=html.document_fromstring(response)   #字符串转换成完整的html对象
# # #doc=etree.HTML(response)
# title=doc.xpath("//div[@class='title']/a")
# time=doc.xpath("//div[@class='time']")
# for i,j in zip(title,time):
#     text=i.xpath(".//text()")[0].strip()
#     # href=i.xpath("@href")[0].strip()
#     time=j.xpath(".//text()")[0].strip()
#     print(text,time)

BeautifulSoup:

import requests
from bs4 import BeautifulSoup

url='http://wjw.gz.gov.cn/ztzl/xxfyyqfk/yqtb/index.html'
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.12151 SLBChan/25'
}
response=requests.get(url,headers=headers).text
# print(response)
soup=BeautifulSoup(response,'html.parser')      #创建文档

#获取中的内容
div_data=soup.find_all('div',class_='cont_list')
# print(div_data)
title_data = soup.find_all('div', class_='title')  #获取标题
time_data = soup.find_all('div', class_='time')     #获取时间
title=title_data[0].find_all('a')
for title in title_data:
    print(title.get_text().strip())
for time in time_data:
    print(time.get_text().strip())

正则re:

# import requests
# import re
# i=0
# url='http://wjw.gz.gov.cn/ztzl/xxfyyqfk/yqtb/index.html'
# headers={
#     'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36 SLBrowser/7.0.0.12151 SLBChan/25'
# }
# response=requests.get(url=url,headers=headers).text
# content=re.compile('.*?(.*?)',re.S)
# results=re.findall(content,response)
# for title in results:
#     print('n标题:'+ title[0]+'n时间:'+title[1].strip())
#     i += 1
# print()




转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/986227.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号