【发布时间】:2021-07-27 13:40:54
【问题描述】:
所以我从这个链接中取了药物的标题:Medicines List
现在我想获取每种药物的内容,同时每种药物都有它自己的链接 例子 : Medicines Example
如何使用 BeautifulSoup4 和请求库获取该药物的内容?
import requests
from bs4 import BeautifulSoup
from pprint import pp
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:90.0) Gecko/20100101 Firefox/90.0'
}
def main(url):
r = requests.get(url, headers=headers)
soup = BeautifulSoup(r.text, 'lxml')
title = [x.text for x in soup.select(
'a[class$=section__item-link]')]
count = 0
for x in range (0, len(title)):
count += 1
print("{0}. {1}\n".format(count, title[x]))
main('https://www.klikdokter.com/obat')
【问题讨论】:
-
从您使用
soup.select找到的链接中获取hrefs,并分别请求他们的页面,并为每个页面发出新的获取请求 -
如果有 500 种不同的药物,我需要为每种药物申请?
标签: python beautifulsoup python-requests