前言
仅供学习
网站截图
源码
#引用requests库
import requests
#引用pyquery库
from pyquery import PyQuery as pq
def WriteTxt(doc):
#拿到html里面所有.c_list_bid
uls=doc('.c_list_bid')
# 拿到所有的li
lis = uls.find('li')
# 拿到所有的a的物体
tagas=lis.find('a').items()
# 以增加模式打开文件
file_handle=open('msg.txt',mode='a', encoding="utf-8")
# 遍历所有的a的物体
for atext in tagas:
#print(atext.text())
file_handle.write(atext.text() + '\n')
# 关闭文件写入
file_handle.close()
return
def GetPQDoc(response):
#使用pq插件拿到可解析的html的源码
html_doc=pq(response.text)
WriteTxt(html_doc)
return
#浏览器头 可直接复制
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 \(KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36'
}
# 遍历1-26
for num in range(1,26):
# 拼接成网址ccgp
indexurl="http://www.****.gov.cn/cggg/zygg/index_"+str(num)+".htm"
print(indexurl)
#拿到html文件
response=requests.get(indexurl,headers=headers)
#设置网站的解码
response.encoding="UTF-8"
GetPQDoc(response)