如何通过卷心python从多个URL中将数据收集到单个项目中
在更简单的术语中,我想从回调函数中获取返回值,直到耗尽的for for for the the the Trafe单项。
我想做的是
我正在创建新链接,该链接表示 https://www.oddsportal.com/soccer/africa/caf-champions-league/al-ahly-es-setif-gkw6i7t6/
例如
基本上,我正在使用一个for循环,并使用dict创建一个新链接并通过回调函数产生请求。
class CountryLinksSpider(scrapy.Spider):
name = 'country_links'
allowed_domains = ['oddsportal.com']
start_urls = ['https://www.oddsportal.com/soccer/africa/caf-champions-league/es-setif-al-ahly-AsWAHRrD/']
def start_requests(self):
for url in self.start_urls:
yield scrapy.Request(url=url, callback=self.create_all_tabs_links_from_url)
def create_all_tabs_links_from_url(self, response):
current_url = response.request.url
_other_useful_scrape_data_dict = OrderedDict(
[('time', '19:00'), ('day', '14'), ('month', 'May'), ('year', '22'), ('Country', 'Africa'),
('League', 'CAF Champions'), ('Home', 'ES Setif'), ('Away', 'Al Ahly'), ('FT1', '2'), ('FT2', '2'),
('FT', 'FT'), ('1H H', '1'), ('1H A', '1'), ('1HHA', 'D'), ('2H H', '1'), ('2H A', 1), ('2HHA', 'D')])
with requests.Session() as s:
s.headers = {
"accept": "*/*",
"accept-encoding": "gzip, deflate, br",
"accept-language": "en-US,en;q=0.9,pl;q=0.8",
"referer": 'https://www.oddsportal.com',
"user-agent": fake_useragent.UserAgent().random
}
r = s.get(current_url)
version_id = re.search(r'"versionId":(\d+)', r.text).group(1)
sport_id = re.search(r'"sportId":(\d+)', r.text).group(1)
xeid = re.search(r'"id":"(.*?)"', r.text).group(1)
xhash = urllib.parse.unquote(re.search(r'"xhash":"(.*?)"', r.text).group(1))
unix = int(round(time.time() * 1000))
tabs_dict = {'#ah;2': ['5-2', 'AH full time', ['1', '2']], '#ah;3': ['5-3', 'AH 1st half', ['1', '2']],
'#ah;4': ['5-4', 'AH 2nd half', ['1', '2']], '#dnb;2': ['6-2', 'DNB full_time', ['1', '2']]}
all_tabs_data = OrderedDict()
all_tabs_data = all_tabs_data | _other_useful_scrape_data_dict
for key, value in tabs_dict.items():
api_url = f'https://fb.oddsportal.com/feed/match/{version_id}-{sport_id}-{xeid}-{value[0]}-{xhash}.dat?_={unix}'
# goto each main tabs and get data from it and yield here
single_tab_scrape_data = yield scrapy.http.Request(api_url,
callback=self.scrape_single_tab)
# following i want to do (collect all the data from all tabs into single item)
# all_tabs_data = all_tabs_data | single_tab_scrape_data # (as a dict)
# yield all_tabs_data # yield single dict with scrape data from all the tabs
def scrape_single_tab(self, response):
# sample scraped data from the response
scrape_dict = OrderedDict([('AH full time -0.25 closing 2', 1.59), ('AH full time -0.25 closing 1', 2.3),
('AH full time -0.25 opening 2', 1.69), ('AH full time -0.25 opening 1', 2.12),
('AH full time -0.50 opening 1', ''), ('AH full time -0.50 opening 2', '')])
yield scrape_dict
我尝试过的东西, 首先,我尝试简单地从Scrape_Match_Data Fuction返回Scrape项目。但是我找不到一种从收益请求中获取回调函数的返回值的方法。
我尝试使用以下库, 来自inline_requests导入inline_requests 从twisted.internet.defer导入inlinecallbacks,
但我无法使其起作用。我觉得必须有更简单的方法将来自不同链接的刮擦项目附加到一个项目中并产生。
请帮助我解决这个问题。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
从技术上讲,我们有两种方法可以在我们使用的回调功能之间传输数据,以从多个请求中构造项目:
1。请求meta字典:
您可能错过了调用
reverse.meta.get('_ scrape_dict')
访问从上一个回调函数中删除的数据2。
cb_kwargs
可用于零工版本1.7和较新的访问:3.求解项目。来自多个类型的响应。
实施它的最简单方法是将数据分配给类变量。
代码看起来像这样:
Technically in scrapy we have 2 approaches to transfer data between callback functions we are using to construct items from multiple requests:
1. Request meta dictionary:
probably You missed to call
response.meta.get('_scrape_dict')
to access data scraped from previous callback function2.
cb_kwargs
accessible for scrapy version 1.7 and newer:3.Single Item from multiple.. responses with the same type.
The easiest way to implement it is to assign data to class variable.
The code will look like this: