我正在写一个垃圾程序,我在这个网站上登录并收集不同扑克牌的数据,http://www.starcitygames.com/buylist/。但我只从这个url中获取ID值,然后使用该ID号重定向到另一个url,然后从JSON网页中获取207种不同类别的卡片。我看起来比直接使用JSON数据访问URL要真实一些。不管怎么说,我以前用多个url编写过Scrapy程序,我可以设置这些程序来轮换代理和用户代理,但我如何在这个程序中做到这一点呢?既然技术上只有一个URL,那么有没有一种方法可以设置它在刮取大约5个不同的JSON数据页之后切换到另一个代理和用户代理?我不想它随机旋转。我想它刮相同的JSON网页与相同的代理和用户代理每次。我希望这一切都有意义。对于堆栈溢出,这可能有点宽泛,但我不知道如何做到这一点,所以我想我会询问任何人,看看是否有人对此有什么好的想法。在
# Import needed functions and call needed python files
import scrapy
import json
from scrapy.spiders import Spider
from scrapy_splash import SplashRequest
from ..items import DataItem
# Spider class
class LoginSpider(scrapy.Spider):
# Name of spider
name = "LoginSpider"
#URL where dated is located
start_urls = ["http://www.starcitygames.com/buylist/"]
# Login function
def parse(self, response):
# Login using email and password than proceed to after_login function
return scrapy.FormRequest.from_response(
response,
formcss='#existing_users form',
formdata={'ex_usr_email': 'example@email.com', 'ex_usr_pass': 'password'},
callback=self.after_login
)
# Function to barse buylist website
def after_login(self, response):
# Loop through website and get all the ID numbers for each category of card and plug into the end of the below
# URL then go to parse data function
for category_id in response.xpath('//select[@id="bl-category-options"]/option/@value').getall():
yield scrapy.Request(
url="http://www.starcitygames.com/buylist/search?search-type=category&id={category_id}".format(category_id=category_id),
callback=self.parse_data,
)
# Function to parse JSON dasta
def parse_data(self, response):
# Declare variables
jsonreponse = json.loads(response.body_as_unicode())
# Call DataItem class from items.py
items = DataItem()
# Scrape category name
items['Category'] = jsonreponse['search']
# Loop where other data is located
for result in jsonreponse['results']:
# Inside this loop, run through loop until all data is scraped
for index in range(len(result)):
# Scrape the rest of needed data
items['Card_Name'] = result[index]['name']
items['Condition'] = result[index]['condition']
items['Rarity'] = result[index]['rarity']
items['Foil'] = result[index]['foil']
items['Language'] = result[index]['language']
items['Buy_Price'] = result[index]['price']
# Return all data
yield items
我给你推荐这个包裹 Scrapy-UserAgents
在你的设置.py文件
^{pr2}$}
要旋转的用户代理示例列表
More User Agents
小心这个中间件不能处理COOKIES启用为True的情况,并且网站将COOKIES与用户代理绑定,这可能会导致蜘蛛的不可预知的结果。在
用户代理: 我使用了此工具,它将始终使用最新和最常用的用户代理更新您的用户代理列表: https://pypi.org/project/shadow-useragent/
代理 我会找一家公司提供一个rotator,这样你就不必费心了,但是你可以编写一个定制的中间件,我会告诉你怎么做的。您要做的是编辑process request方法。这样既可以更改代理,也可以更改用户代理。在
用户代理 您可以使用Scrapy random user-agent中间件https://github.com/cleocn/scrapy-random-useragent,或者这就是如何使用中间件(包括代理或任何其他报头)更改请求对象的任何内容。在
参考文献: https://docs.scrapy.org/en/latest/topics/request-response.html
相关问题 更多 >
编程相关推荐