【发布时间】:2016-02-19 05:09:05
【问题描述】:
有人可以指导我,如何使用 python 获取视图页面源中可见的所有链接。我想从所有标签中检索所有链接(如链接、a、img、css...一切)。下面是我尝试过的代码。
import requests
from bs4 import BeautifulSoup
r=requests.get(url)
soup = BeautifulSoup(r.content)
soup.prettify()
for anchor in soup.find_all('a',href=True):
print anchor['href']
for anchor in soup.find_all('link',href=True):
print anchor['href']
for anchor in soup.find_all('img',src=True):
print anchor['src']
for anchor in soup.find_all('script',src=Treu):
print anchor['src']
像这样我能够从所有标签中获取链接,但无法从样式表中获取链接。例如 .bg {.bg {背景:网址(XXXX)}。
【问题讨论】:
-
BeautifulSoup 帮不了你。你考虑过正则表达式吗?还有一个警告说明:如果 javascript 或 css 嵌入在 HTML 中(而不是在外部文件中),并且它们引用了其他 url,那么您现在使用当前的方法也会丢失它。
标签: python beautifulsoup python-requests