在我的网络抓取项目中,我必须从https://www.national-football-teams.com/country/67/2018/France.html中抓取足球比赛数据,以便从上面的url导航到比赛数据,我必须遵循url中具有散列的超引用:
<a href="#matches" data-toggle="tab">Matches</a>event以下链接的标准抓取机制:
href = response.xpath("//a[contains(@href,'matches')]/@href").extract_first()
href = response.urljoin(href)将生成一个不指向匹配数据的链接:https://www.national-football-teams.com/matches.html
如果有任何帮助,我将不胜感激。由于我是网络抓取和任何与网络开发有关的新手,所以一个更具体的建议和/或一个最小的工作示例是高度认可的。为了完整,下面是我的scrapy-spider的完整代码:
import scrapy
class NationalFootballTeams(scrapy.Spider):
name = "nft"
start_urls = ['https://www.national-football-teams.com/continent/1/Europe.html']
def parse(self, response):
for country in response.xpath("//div[@class='row country-teams']/div[1]/ul/li/a"):
cntry = country.xpath("text()").extract_first().strip()
if cntry == 'France':
href = country.xpath("@href").extract_first()
yield response.follow(href, self.parse_country)
def parse_country(self, response):
href = response.xpath("//a[contains(@href,'matches')]/@href").extract_first()
href = response.urljoin(href)
print href
yield scrapy.Request(url=href, callback=self.parse_matches)
def parse_matches(self, response):
print response.xpath("//tr[@class='win']").extract()发布于 2018-10-07 20:42:07
单击该链接时,不会加载任何新页面,甚至不会加载新数据,它已经在html中,但处于隐藏状态。单击该链接将调用一些隐藏当前选项卡并显示新选项卡的javascript。因此,要获得数据,不需要使用任何链接,只需使用不同的xpath查询即可。匹配数据在xpath //div[@id='matches']中。
https://stackoverflow.com/questions/52688518
复制相似问题