【发布时间】:2020-12-25 19:26:20
【问题描述】:
我遇到了一项任务,我必须使用爬虫从网站检索信息。 (网址:https://www.onepa.gov.sg/cat/adventure)
该网站有多种产品。对于每个产品,它都包含将我们定向到该单个产品网页的链接,我想收集所有链接。
例如,其中一个产品的名称为:KNOTTY STUFF,我希望得到 /class/details/c026829364 的 href
import requests
from bs4 import BeautifulSoup
def get_soup(url):
source_code = requests.get(url)
plain_text = source_code.text
soup = BeautifulSoup(plain_text, features="html.parser")
return soup
url = "https://www.onepa.gov.sg/cat/adventure"
soup = get_soup(url)
for i in soup.findAll("a", {"target": "_blank"}):
print(i.get("href"))
输出为https://tech.gov.sg/report_vulnerability https://www.pa.gov.sg/feedback
其中不包括我正在寻找的内容:/class/details/c026829364
感谢任何帮助或帮助,谢谢!
【问题讨论】:
-
尝试在
plain_text中搜索/class/details/c026829364。 -
我会使用scrapy。
标签: python beautifulsoup web-crawler