gpt4 book ai didi

Python Scrapy 和产量

转载 作者:太空宇宙 更新时间:2023-11-03 10:55:54 26 4
gpt4 key购买 nike

我目前是第一次使用 Scrapy 开发一个爬虫,我也是第一次使用 Yield。我仍在努力思考 yield 。

抓取刀:

  • 抓取一页以获取日期列表(解析)
  • 使用这些日期来格式化 URLS,然后抓取(parse_page_contents)
  • 在此页面上,它找到每个单独列表的 URL 并抓取单个列表 (parse_page_listings)
  • 在个人列表中,我想提取所有数据。每个单独的列表上还有 4 个链接,其中包含更多数据。 (parse_individual_listings)

我正在努力理解如何将来自 parse_individual_tabs 和 parse_individual_listings 的 JSON 组合成一个 JSON 字符串。这将是每个单独列表的一个,并将被发送到 API。即使只是暂时打印它也可以。

    class MySpider(scrapy.Spider):
name = "myspider"

start_urls = [
'',
]


def parse(self, response):
rows = response.css('table.apas_tbl tr').extract()
for row in rows[1:]:
soup = BeautifulSoup(row, 'lxml')
dates = soup.find_all('input')
url = ""
yield scrapy.Request(url, callback=self.parse_page_contents)

def parse_page_contents(self, response):
rows = response.xpath('//div[@id="apas_form"]').extract_first()
soup = BeautifulSoup(rows, 'lxml')
pages = soup.find(id='apas_form_text')
urls = []
urls.append(response.url)
for link in pages.find_all('a'):
urls.append('/'.format(link['href']))

for url in urls:
yield scrapy.Request(url, callback=self.parse_page_listings)

def parse_page_listings(self, response):
rows = response.xpath('//div[@id="apas_form"]').extract_first()
soup = BeautifulSoup(rows, 'lxml')
resultTable = soup.find("table", { "class" : "apas_tbl" })

for row in resultTable.find_all('a'):
url = ""
yield scrapy.Request(url, callback=self.parse_individual_listings)


def parse_individual_listings(self, response):
rows = response.xpath('//div[@id="apas_form"]').extract_first()
soup = BeautifulSoup(rows, 'lxml')
fields = soup.find_all('div',{'id':'fieldset_data'})
for field in fields:
print field.label.text.strip()
print field.p.text.strip()

tabs = response.xpath('//div[@id="tabheader"]').extract_first()
soup = BeautifulSoup(tabs, 'lxml')
links = soup.find_all("a")
for link in links:
yield scrapy.Request( urlparse.urljoin(response.url, link['href']), callback=self.parse_individual_tabs)

收件人:

def parse_individual_listings(self, response): 
rows = response.xpath('//div[@id="apas_form"]').extract_first()
soup = BeautifulSoup(rows, 'lxml')
fields = soup.find_all('div',{'id':'fieldset_data'})
data = {}
for field in fields:
data[field.label.text.strip()] = field.p.text.strip()

tabs = response.xpath('//div[@id="tabheader"]').extract_first()
soup = BeautifulSoup(tabs, 'lxml')
links = soup.find_all("a")
for link in links:
yield scrapy.Request(
urlparse.urljoin(response.url, link['href']),
callback=self.parse_individual_tabs,
meta={'data': data}
)
print data

..

    def parse_individual_tabs(self, response): 
data = {}
rows = response.xpath('//div[@id="tabContent"]').extract_first()
soup = BeautifulSoup(rows, 'lxml')
fields = soup.find_all('div',{'id':'fieldset_data'})
for field in fields:
data[field.label.text.strip()] = field.p.text.strip()

print json.dumps(data)

def parse_individual_tabs(self, response): 
data = {}
rows = response.xpath('//div[@id="tabContent"]').extract_first()
soup = BeautifulSoup(rows, 'lxml')
fields = soup.find_all('div',{'id':'fieldset_data'})
for field in fields:
data[field.label.text.strip()] = field.p.text.strip()

yield json.dumps(data)

最佳答案

通常在获取数据时,你必须使用Scrapy Items但它们也可以替换为字典(即您所指的 JSON 对象),因此我们现在将使用它们:

首先,开始在 parse_individual_listings 方法中创建项目(或字典),就像在 parse_individual_tabs 中创建 data 一样。然后将它传递给下一个请求(它将被 parse_individual_tabsmeta 参数捕获,所以它应该看起来像:

def parse_individual_listings(self, response):
...
data = {}
data[field1] = 'data1'
data[field1] = 'data2'
...
yield scrapy.Request(
urlparse.urljoin(response.url, link['href']),
callback=self.parse_individual_tabs,
meta={'data': data};
)

然后,您可以在 parse_individual_tabs 中获取该数据:

def parse_individual_tabs(self, response):
data = response.meta['data']
...
# keep populating `data`
yield data

现在 parse_individual_tabs 中的 data 拥有您想要从两个请求中获得的所有信息,您可以在任何回调请求之间执行相同的操作。

关于Python Scrapy 和产量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40936117/

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