-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathdemo_scrapy.py
33 lines (26 loc) · 956 Bytes
/
demo_scrapy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import scrapy
from config import API_KEY
from twocaptcha import TwoCaptcha
from scrapy.exceptions import CloseSpider
solver = TwoCaptcha(API_KEY)
sitekey = '6LfGNEoeAAAAALUsU1OWRJnNsF1xUvoai0tV090n'
url = 'https://www.scrapebay.com/spam'
class CaptchaSpider(scrapy.Spider):
name = 'captcha'
start_urls = [url]
def parse(self, response):
try:
result = solver.recaptcha(sitekey=sitekey, url=url)
except Exception as e:
raise CloseSpider('Could not solve captcha')
captcha = result.get('code')
payload = {
'g-recaptcha-response': captcha
}
yield scrapy.FormRequest.from_response(response,
formdata = payload,
callback = self.parse_page)
def parse_page(self, response):
yield {
'data': response.css('td:last-child ::text').get()
}