擅长:python、mysql、java
<pre><code>import requests
import bs4
import lxml
result=requests.get("https://www.1mg.com/drugs-all-medicines")
soup = bs4.BeautifulSoup(result.text,"lxml")
box = soup.find('a', attrs={"href": "/drugs/augmentin-625-duo-tablet-138629"})
text_content = box.find('span')
for paragraph in text_content.find_all('p'):
print(paragraph.text)
price = text_content.find('div').find('span').text
print(price)
</code></pre>
<p>输出:</p>
<pre><code>Augmentin 625 Duo Tablet
Prescription Required
strip of 10 tablets
Glaxo SmithKline Pharmaceuticals Ltd
Amoxycillin (500mg) + Clavulanic Acid (125mg)
MRP ₹200.59
</code></pre>
<p>首先,确定包含数据的框。然后得到包含文本的跨度。对于范围内的每个段落,打印文本</p>
<p>如果对价格感兴趣,请进入div并进一步了解其范围</p>