发布于2019-08-05 11:43 阅读(854) 评论(0) 点赞(3) 收藏(3)
博问上有人不会,我写了一下
绝对不要加多线程多线程进去
import re
import requests
from lxml.html import etree
url = 'http://www.liyang.gov.cn/default.php?mod=article&fid=163250&s99679207_start=0'
rp = requests.get(url)
re_html = etree.HTML(rp.text)
url_xpath = '//*[@id="s99679207_content"]/table/tbody/tr/td/span[1]/span/a/@href'
title_xpath = '//*[@id="s99679207_content"]/table/tbody/tr/td/span[1]/span/a/text()'
url_list = re_html.xpath(url_xpath)
title_list = re_html.xpath(title_xpath)
title_list = title_list[::-1]
data_url_list = []
for url_end in url_list:
new_url = f'http://www.liyang.gov.cn/{url_end}'
print(new_url)
rp_1 = requests.get(new_url)
print(rp_1.text)
try:
re_1_html = etree.HTML(rp_1.text)
data_url_xpth = '//tbody/tr[1]/td[2]/a'
data_url = re_1_html.xpath(data_url_xpth)[0]
except:
data_list = re.findall('<a href="(.*?)" target="_blank">', rp_1.text)
data_url = data_list[0]
print(data_url)
data_url = f'http://www.liyang.gov.cn/{data_url}'
re = requests.get(data_url)
data = re.content
with open(f'{title_list.pop()}.pdf', 'wb') as fw:
fw.write(data)
作者:丑八怪
链接:https://www.pythonheidong.com/blog/article/4413/7f05e390d44a4fc8b6d4/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!