Python 如何清除网页中的所有链接?我的代码只删除了一些链接
这是我用来清除网页中所有链接的代码:Python 如何清除网页中的所有链接?我的代码只删除了一些链接,python,html,web-scraping,beautifulsoup,Python,Html,Web Scraping,Beautifulsoup,这是我用来清除网页中所有链接的代码: from bs4 import BeautifulSoup import requests import re page = requests.get("http://www3.asiainsurancereview.com/News") soup = BeautifulSoup(page.text, "html.parser") for link in soup.findAll('a', attrs={'href': re.compile("^http:
from bs4 import BeautifulSoup
import requests
import re
page = requests.get("http://www3.asiainsurancereview.com/News")
soup = BeautifulSoup(page.text, "html.parser")
for link in soup.findAll('a', attrs={'href': re.compile("^http://")}):
print(link.get('href'))
links.close()
但它只列出下拉列表中存在的链接。为什么呢?为什么它没有“看到”页面上新闻文章的链接?我真的想把所有的新闻文章都删掉。我尝试了以下方法,以识别标记并刮取该标记内的新闻文章链接:
import requests
import re
links=open("Life_and_health_links.txt", "a")
page = requests.get("http://www3.asiainsurancereview.com/News")
soup = BeautifulSoup(page.text, "html.parser")
li_box = soup.select('div.col-sm-5 > ul > li > h5 > a')
for link in li_box:
print(link['href'])
当然,这只显示特定标记中的链接。要列出其他标记中的链接,我必须多次运行此代码,指定要列出其链接的特定标记。但是,如何在所有标签中列出新闻文章的所有链接,并跳过不属于新闻文章的链接?您需要进行一些研究,以找到新闻链接的常见模式 试试这个,希望能奏效
li_box = soup.select("div ul li h5 a")
for a in li_box:
print(a['href'])