从URL中抓取数据:如何检索带有丢失和未知最终页面id的所有URL页面

2024-06-25 23:27:22 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把一组网页的数据拉下来。在

以下是URL的示例:

http://www.signalpeptide.de/index.php?sess=&m=listspdb_mammalia&s=details&id=3&listname=

我的问题是:

  1. URL中的“id=”编号在不同页面之间更改。在
  2. 我想遍历并检索数据库中的所有页面。在
  3. 会缺少id(例如,可能会有一个id=3和id=6的页面,但不会出现id=4和id=5的页面)。在
  4. 我不知道id的最终数量(例如,数据库中的最后一页可能是id=100000或id=1000000000,我不知道)。在

我知道我需要的两行代码是以某种方式列出一个数字列表,然后用这个代码循环通过这些数字,以下拉出每页的文本(解析文本本身是另一天的工作):

import urllib2
from bs4 import BeautifulSoup
web_page = "http://www.signalpeptide.de/index.php?sess=&m=listspdb_mammalia&s=details&id=" + id_name + "&listname="
page = urllib2.urlopen(web_page)
 soup = BeautifulSoup(page,'html.parser')

有谁能给我一个建议,说“把所有的页面都拿走”来解决我面临的问题吗?因为我不知道最后一页是什么时候?在


Tags: idhttpurlindexwwwpagede页面
1条回答
网友
1楼 · 发布于 2024-06-25 23:27:22

为了获得可能的页面,您可以执行以下操作(我的示例是Python3):

import re
from urllib.request import urlopen
from lxml import html

ITEMS_PER_PAGE = 50

base_url = 'http://www.signalpeptide.de/index.php'
url_params = '?sess=&m=listspdb_mammalia&start={}&orderby=id&sortdir=asc'


def get_pages(total):
    pages = [i for i in range(ITEMS_PER_PAGE, total, ITEMS_PER_PAGE)]
    last = pages[-1]
    if last < total:
        pages.append(last + (total - last))
    return pages

def generate_links():
    start_url = base_url + url_params.format(ITEMS_PER_PAGE)
    page = urlopen(start_url).read()
    dom = html.fromstring(page)
    xpath = '//div[@class="content"]/table[1]//tr[1]/td[3]/text()'
    pagination_text = dom.xpath(xpath)[0]
    total = int(re.findall(r'of\s(\w+)', pagination_text)[0])
    print(f'Number of records to scrape: {total}')
    pages = get_pages(total)
    links = (base_url + url_params.format(i) for i in pages)
    return links

基本上,它所做的是获取第一页并获取记录数,假设每页有50条记录,get_pages()函数可以计算传递给start参数的页码并生成所有分页URL,您需要获取所有这些页,用每个蛋白质迭代表,然后转到details页面获取使用beauthulsoup或lxml和XPath所需的信息。我尝试使用asyncio同时获取所有这些页面,但服务器超时:)。希望我的功能有帮助!在

相关问题 更多 >