【问题标题】:How to extract data from many pages at the same time using python?如何使用python同时从多个页面中提取数据?
【发布时间】:2018-02-23 18:51:36
【问题描述】:

我使用python 3.6,我尝试从页面中提取数据,但我想同时从多个页面中提取数据,代码如下:

   page = requests.get('http://www.poetsgate.com/ViewPoem.aspx?id=12343')
   tree = html.fromstring(page.content)
   text1 = tree.xpath('//div[@class="col1 first"]/text()')
   text2 = tree.xpath('//div[@class="col2 second"]/text()')

有什么方法可以提取数据,但我不喜欢使用页面的所有 URL 列表!

【问题讨论】:

  • 没有 URL 列表?您可以执行递归wget。除非您有权访问该网站的数据库,否则我不知道您将如何确定使用哪个 ?id=s。

标签: python extract extraction text-extraction data-extraction


【解决方案1】:

如果没有更多详细信息,我认为不使用 URL 列表是不可能的(无论您是手工制作还是以编程方式获取它们是另一个问题;)。

我建议使用helper function 来处理composable 列表:

url_list = ["http://example.com/route/page", ...]
for url in url_list:
    output = extract_data(url)
    do_something(output)

def extract_data(url):
   page = requests.get(url)
   tree = html.fromstring(page.content)
   text1 = tree.xpath('//div[@class="col1 first"]/text()')
   text2 = tree.xpath('//div[@class="col2 second"]/text()')
   return text1, text2

【讨论】:

  • 有没有办法以编程方式获取所有 URL?例如,我想从这些 URL 中提取数据,但我不喜欢进入每个 URL 来复制它并在列表中过去做某事,poetsgate.com/Poet.aspx?id=45&type=cat&typeid=5
  • @RazanBalatiah 这可能是它自己的问题,但它类似于@alex 上面的建议。你可以遍历所有?id=$i,但我猜你会遇到很多垃圾或404,所以要做好准备。或者跟踪您找到的?id= 链接,并将它们全部批处理。但我感觉你知道自己想要什么,但我不明白你为什么不能使用列表。
猜你喜欢
  • 2019-10-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-01
相关资源
最近更新 更多