使用python从同一网页内的多个链接提取数据

使用python从同一网页内的多个链接提取数据,python,web-scraping,beautifulsoup,python-requests,Python,Web Scraping,Beautifulsoup,Python Requests,我是python和web抓取的新手 我试图从这个链接中提取有关临床诊断测试的测试组件的信息https://labtestsonline.org/tests-index 测试索引中列出了各种临床测试的测试组件名称。单击这些名称中的每一个将带您进入另一个页面,该页面包含关于单个测试组件的详细信息。从这一页我想摘录的部分有共同的问题 最后,将包含测试组件名称的数据框放在一列中,并将常见问题中的每个问题作为其余列(如下所示) 到目前为止,我只获得了测试组件的名称 import requests from

我是python和web抓取的新手

我试图从这个链接中提取有关临床诊断测试的测试组件的信息<代码>https://labtestsonline.org/tests-index

测试索引中列出了各种临床测试的测试组件名称。单击这些名称中的每一个将带您进入另一个页面,该页面包含关于单个测试组件的详细信息。从这一页我想摘录的部分有共同的问题

最后,将包含测试组件名称的数据框放在一列中,并将常见问题中的每个问题作为其余列(如下所示)

到目前为止,我只获得了测试组件的名称

import requests
from bs4 import BeautifulSoup
url = 'https://labtestsonline.org/tests-index'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml' )
print(soup.prettify())
l = [] #get the names of the test components from the index
for i in soup.select("a[hreflang*=en]"):
l.append(i.text)
import pandas as pd
names = pd.DataFrame({'col':l})  # convert the above list to a dataframe

我建议你看看开源的网页抓取库。它将帮助您解决在抓取网站时可能遇到的许多问题,例如:

  • 下面是每个页面上的链接
  • 从与特定模式匹配的页面中刮取数据,例如,您可能只想刮取/详细信息页面,而其他页面只需刮取链接即可进行爬网
  • lxml和css选择器
  • 并发性,允许您同时抓取多个页面,这将大大加快您的抓取速度
这很容易开始,而且有很多关于如何使用Scrapy库构建从简单到高级的web scraper的资源

import requests
from bs4 import BeautifulSoup
url = 'https://labtestsonline.org/tests-index'
response = requests.get(url)
soup = BeautifulSoup(response.content, 'lxml' )
print(soup.prettify())
l = [] #get the names of the test components from the index
for i in soup.select("a[hreflang*=en]"):
l.append(i.text)
import pandas as pd
names = pd.DataFrame({'col':l})  # convert the above list to a dataframe