1 需求
批量爬取巨潮资讯网中“贵州茅台”相关公告的PDF文件。
2 代码实现
import re
import requests
from selenium import webdriver
# 获取网页源代码
chrome_options = webdriver.ChromeOptions()
chrome_options.add_argument('--headless')
browser = webdriver.Chrome(options=chrome_options)
url = 'http://www.cninfo.com.cn/new/disclosure/stock?stockCode=600519&orgId=gssh0600519#latestAnnouncement'
browser.get(url=url)
data = browser.page_source
# 获取网页标题
p_title = '<span class="r-title">(.*?)</span>'
title = re.findall(p_title, data)
# 获取网页网址
p_href = '<div class="cell"><a target="_blank" href="(.*?)" data-id='
href = re.findall(p_href, data)
for index in range(len(href)):
# 网址清理
href[index] = 'http://www.cninfo.com.cn' + href[index]
href[index] = re.sub('amp;', '', href[index])
# pdf文件下载
res = requests.get(url=href[index])
path = "问询函//" + title[index] + ".pdf"
file = open(path, 'wb')
file.write(res.content)
file.close()
print("第" + str(index + 1) + "号文件爬取成功!")