gpt4 book ai didi

Python scrapy reddit 确认按钮中断抓取

转载 作者:太空宇宙 更新时间:2023-11-03 11:24:19 24 4
gpt4 key购买 nike

我正在尝试抓取网站数据,当我访问 18 岁以上的页面时,我收到一个警告页面。我的爬虫在大多数 reddit 页面上正常工作,我可以成功获取数据。我尝试使用 selenium 移动到下一页,当它打开浏览器时成功,但爬虫没有跟随到该页面。下面是我的代码..

class DarknetmarketsSpider(scrapy.Spider):
name = "darknetmarkets"
allowed_domains = ["https://www.reddit.com"]
start_urls = (
'http://www.reddit.com/r/darknetmarkets',
)
rules = (Rule(LinkExtractor(allow=()), callback='parse_obj', follow=False),)
def __init__(self):
self.driver = webdriver.Firefox()

def parse(self, response):
self.driver.get('http://www.reddit.com/r/darknetmarkets')
#self.driver.get('https://www.reddit.com/over18?dest=https%3A%2F%2Fwww.reddit.com%2Fr%2Fdarknetmarketsnoobs')

while True:
try:
YES_BUTTON = '//button[@value="yes"]'
next = self.driver.find_element_by_xpath(YES_BUTTON).click()


url = 'http://www.reddit.com/r/darknetmarkets'

next.click()
except:
break

self.driver.close()


item = darknetItem()
item['url'] = []
for link in LinkExtractor(allow=(), deny=self.allowed_domains).extract_links(response):
item['url'].append(link.url)
print link

按钮的CSS..

<button class="c-btn c-btn-primary" type="submit" name="over18" value="yes">continue</button>

最佳答案

我看到您正试图绕过该 subreddit 中的年龄限制屏幕。单击“继续”按钮后,该选择将保存为 cookie,因此您必须返回到 scrapy。

在你用Selenium点击它之后保存cookies并将它们发送到scrapy

代码由 scrapy authentication login with cookies 提供

class MySpider(scrapy.Spider):
name = 'MySpider'
start_urls = ['http://reddit.com/']

def get_cookies(self):
self.driver = webdriver.Firefox()
base_url = "http://www.reddit.com/r/darknetmarkets/"
self.driver.get(base_url)
self.driver.find_element_by_xpath("//button[@value='yes']").click()
cookies = self.driver.get_cookies()
self.driver.close()
return cookies

def parse(self, response):
yield scrapy.Request("http://www.reddit.com/r/darknetmarkets/",
cookies=self.get_cookies(),
callback=self.darkNetPage)

关于Python scrapy reddit 确认按钮中断抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36994417/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com