源代码:

  1. #2022-03-02 xpath爬取通州区人民政府网站招聘信息
  2. import requests
  3. from lxml import etree
  4. import time
  5. for i in range(5):#一共有13
  6. if i==1:
  7. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml'
  8. else:
  9. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index_{}.shtml'.format(i)
  10. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
  11. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml'
  12. response=requests.get(url=url,headers=headers)
  13. response.encoding = 'utf-8'
  14. html=etree.HTML(response.text)
  15. lis=html.xpath('//*[@id="channelNames"]/li')#拿到所有的li
  16. for li in lis:
  17. title=li.xpath('./a/text()')[0]#获取标题
  18. dt=li.xpath('./span/text()')[0]#获取日期
  19. href=li.xpath('./a/@href')[0]#获取页面链接
  20. href1=href.split(".")[-1]#以点来分割,往后取第一个元素
  21. if href1=="shtml":
  22. href="http://www.bjtzh.gov.cn"+href#拼接起来
  23. else:
  24. pass
  25. print(title,dt,href)
  26. time.sleep(3)
  27. with open(r"通州news.txt","a",encoding="utf-8") as f: #使用with open()新建对象f ,a 表示追加
  28. f.write("{},{},{}".format(title,dt,href)) #将列表中的数据循环写入到文本文件中
  29. f.write("\n")#换行

分析:

1、访问网站信息

url=http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml

  1. import requests
  2. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
  3. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml'
  4. response=requests.get(url=url,headers=headers)
  5. response.encoding = 'utf-8'
  6. print(response.text)

这个打印出来的是乱码的,加一句代码:response.encoding = ‘utf-8’

2、解析数据,拿到所有li标签

  1. import requests
  2. from lxml import etree
  3. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
  4. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml'
  5. response=requests.get(url=url,headers=headers)
  6. response.encoding = 'utf-8'
  7. html=etree.HTML(response.text)
  8. lis=html.xpath('//*[@id="channelNames"]/li')#拿到所有的li
  9. print(lis)

image.png

copy得到的xpath是://*[@id=”channelNames”]/li[1],需要的是所有的li,所以把[1]去掉

3、找到单个li,并查找所有需要的字段

  1. import requests
  2. from lxml import etree
  3. import time
  4. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
  5. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml'
  6. response=requests.get(url=url,headers=headers)
  7. response.encoding = 'utf-8'
  8. html=etree.HTML(response.text)
  9. lis=html.xpath('//*[@id="channelNames"]/li')#拿到所有的li
  10. for li in lis:
  11. title=li.xpath('./a/text()')[0]#获取标题
  12. dt=li.xpath('./span/text()')[0]#获取日期
  13. href=li.xpath('./a/@href')[0]#获取页面链接
  14. print(title,dt,href)
  15. time.sleep(1)

输出结果为:
image.png

由输出结果可以看出,部分href链接异常,格式不统一,所以要加一个if判断条件

  1. import requests
  2. from lxml import etree
  3. import time
  4. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
  5. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml'
  6. response=requests.get(url=url,headers=headers)
  7. response.encoding = 'utf-8'
  8. html=etree.HTML(response.text)
  9. lis=html.xpath('//*[@id="channelNames"]/li')#拿到所有的li
  10. for li in lis:
  11. title=li.xpath('./a/text()')[0]#获取标题
  12. dt=li.xpath('./span/text()')[0]#获取日期
  13. href=li.xpath('./a/@href')[0]#获取页面链接
  14. href1=href.split(".")[-1]#以点来分割,往后取第一个元素
  15. if href1=="shtml":
  16. href="http://www.bjtzh.gov.cn"+href#拼接起来
  17. else:
  18. pass
  19. print(title,dt,href)
  20. time.sleep(1)

输出结果为:
image.png
image.png

其他字段也是一样的方法:点击按钮,在网页中点击你想查找的部分,在Elements对应代码中点击右键,Copy->Copy Xpath

4、保存数据

  1. import requests
  2. from lxml import etree
  3. import time
  4. for i in range(5):#一共有13页,可以直接改成13,这里只试了5
  5. if i==1:
  6. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml'
  7. else:
  8. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index_{}.shtml'.format(i)
  9. headers={'User-Agent':'Mozilla/5.0 (iPhone; CPU iPhone OS 13_2_3 like Mac OS X) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.3 Mobile/15E148 Safari/604.1'}
  10. url='http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml'
  11. response=requests.get(url=url,headers=headers)
  12. response.encoding = 'utf-8'
  13. html=etree.HTML(response.text)
  14. lis=html.xpath('//*[@id="channelNames"]/li')#拿到所有的li
  15. for li in lis:
  16. title=li.xpath('./a/text()')[0]#获取标题
  17. dt=li.xpath('./span/text()')[0]#获取日期
  18. href=li.xpath('./a/@href')[0]#获取页面链接
  19. href1=href.split(".")[-1]#以点来分割,往后取第一个元素
  20. if href1=="shtml":
  21. href="http://www.bjtzh.gov.cn"+href#拼接起来
  22. else:
  23. pass
  24. print(title,dt,href)
  25. time.sleep(3)
  26. with open(r"通州news.txt","a",encoding="utf-8") as f: #使用with open()新建对象f ,a 表示追加
  27. f.write("{},{},{}".format(title,dt,href)) #将列表中的数据循环写入到文本文件中
  28. f.write("\n")#换行

输出结果为:(3页的数据)
image.png
text文件内容为:
image.png
image.png
image.png

1、分析网站url可得到,第一页是固定:http://www.bjtzh.gov.cn/bjtz/home/zpxx/index.shtml,从第二页开始就不一样的格式,所以用一个if条件判断,最后用for循环来保存每一页数据 2、用with open来打开一个对象文件

5、爬取完成

注意:
1、这种最简单的访问方式,很容易导致ip被封,请谨慎执行!
(执行3-5次,好像没啥问题,执行多了,就封了。。。)
2、可以尝试使用selenium方式来访问