Python 如何提取包含链接列表的表中的可下载链接,并刮取多个页面
我想在单击表中的特定标题(即本例中的公告)时提取所有.doc链接 我能够提取第一级中的标题、日期和所有链接,只提取一页,如下代码所示:Python 如何提取包含链接列表的表中的可下载链接,并刮取多个页面,python,selenium,web-scraping,beautifulsoup,python-requests,Python,Selenium,Web Scraping,Beautifulsoup,Python Requests,我想在单击表中的特定标题(即本例中的公告)时提取所有.doc链接 我能够提取第一级中的标题、日期和所有链接,只提取一页,如下代码所示: from lxml import etree from selenium import webdriver from selenium.webdriver.chrome.options import Options import sys import pandas as pd from urllib.request import urlparse, urljoi
from lxml import etree
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import sys
import pandas as pd
from urllib.request import urlparse, urljoin
from bs4 import BeautifulSoup
import requests
frame =[]
chrome_options = Options()
chrome_options.add_argument('--headless')
chrome_options.add_argument('--disable-gpu')
driver = webdriver.Chrome(options = chrome_options)
for page_number in range(1,78):
url = 'http://example.com/index{}.html'.format(page_number)
driver.get(url)
html = etree.HTML(driver.page_source)
extract_announcements_list = html.xpath('//table[@id="14681"]/tbody/tr/td/table[@width="90%"][position()>=2 and position() <= (last())]')
for i in list:
date = i.xpath('./tbody/tr/td[3]/text()')
title = i.xpath('./tbody/tr/td[2]/font/a/@title')
link = i.xpath('./tbody/tr/td[2]/font/a/@href')
real_link = 'http://example.com'+ link[0]
print(title,date,real_link)
frame.append({
'title': title,
'link': real_link,
'date': date,
**'content': doc_link,** #this is the doc_link I want to extract in the second level
})
dfs = pd.DataFrame(frame)
dfs.to_csv('myscraper.csv',index=False,encoding='utf-8-sig')
从lxml导入etree
从selenium导入webdriver
从selenium.webdriver.chrome.options导入选项
导入系统
作为pd进口熊猫
从urllib.request导入urlparse、urljoin
从bs4导入BeautifulSoup
导入请求
帧=[]
chrome_options=options()
chrome_选项。添加_参数('--headless')
chrome_选项。添加_参数('--disable gpu')
driver=webdriver.Chrome(选项=Chrome\u选项)
对于范围(1,78)内的页码:
url='1〕http://example.com/index{}.html'.格式(页码)
获取驱动程序(url)
html=etree.html(driver.page\u源代码)
extract\u announcements\u list=html.xpath('//table[@id=“14681”]/tbody/tr/td/table[@width=“90%”][position()>=2和position()=2和position()您需要在代码中缩进这一部分,以便append函数处理所有的刮取数据。我相信这也是@arundeep chohan试图强调的
frame.append({
'title': announcement_title,
'link': real_link,
'date': announcement_date,
**'content': doc_link,** #this is the doc_link I want to extract in the second level
})
查找文档文件的逻辑如下。请修改并使用它。这是我用来下载pdf文件的代码的一部分
for link in soup.findAll('a'):
theLink = link.get('href')
name= link.string
# Logic to find .pdf files
if theLink[-4:] == ".pdf" or theLink[-4:] == ".pdf":
if theLink[-4:] ==".pdf":
fileExtension = ".pdf"
为什么不是所有的东西都在for循环中?您只得到最后一页。对于范围(1,78)中的页码:@arundeepchohan之前,我可以将此代码用于范围(1,78)中的页码:它在其他页面上运行得很好,我只是不太确定我在这里做错了什么,我只能用Python获取最后一页,当导出到csv文件时,只会显示最后一页的最后一个公告+日期+链接。我想我可能会加入循环或其他什么。非常感谢你能帮我完成这项工作。你呢应该把它放在for循环中。你每次循环并重新初始化值,然后只追加一次。非常感谢@arundeepchohan。它现在可以工作了。只是想知道,你能不能建议如何修改我的代码,在单击表中的链接时丢弃所有网页并获取所有.doc链接?有吗如果您能提供帮助,我们将不胜感激。@arundeepchohan更新:我刚刚通过按顺序缩进修改了我的代码。我现在可以删除所有页面。现在问题只剩下提取所有的.doc链接。非常感谢您能给我提些建议。我的错,我一开始没有意识到。它现在正在按预期工作。非常好g、 如果您能建议如何修改我的代码,以便在单击表中的链接时删除所有网页并获取所有.doc链接,我们将不胜感激。由于该网站使用的是外语,因此很难导航页面并进行调试。但是,我确实注意到存在xlsx文件,并且这些文件都是l作为链接存储在网站上。您可以查找扩展名为.doc或.docx的带有“href”的标记,并获得所有URL/可能还下载文件。更新:我刚刚修改了代码,按顺序缩进。我现在可以废弃所有页面。现在,提取所有.doc链接的问题刚刚解决。非常感谢您的建议请告诉我。@Tracy我已经更新了上面的代码,其中包含了一段可以帮助您找到文档文件的代码。只需适当地使用扩展名..doc或.docx谢谢,您的编辑对我来说真的很重要。我只是想知道您是否可以看看我更新的代码,因为我在运行此程序时出错。我想这是由于发送的请求o这一页。我试着把前两页删掉。
for link in soup.findAll('a'):
theLink = link.get('href')
name= link.string
# Logic to find .pdf files
if theLink[-4:] == ".pdf" or theLink[-4:] == ".pdf":
if theLink[-4:] ==".pdf":
fileExtension = ".pdf"