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()