我有下面的代码,当你把一个网址与一堆链接,它会返回列表给你。这工作得很好,除了我只希望链接,以。。。这将返回每个链接,包括home/back/等。有没有方法使用通配符或“start with”函数?你知道吗
from bs4 import BeautifulSoup
import requests
url = ""
# Getting the webpage, creating a Response object.
response = requests.get(url)
# Extracting the source code of the page.
data = response.text
# Passing the source code to BeautifulSoup to create a BeautifulSoup object for it.
soup = BeautifulSoup(data, 'lxml')
# Extracting all the <a> tags into a list.
tags = soup.find_all('a')
# Extracting URLs from the attribute href in the <a> tags.
for tags in tags:
print(tags.get('href'))
还有,有没有导出到excel的方法?我对python不是很在行,老实说,我也不知道我是怎么做到这一步的。你知道吗
谢谢你
关于你的第二个问题:有没有导出到Excel的方法-我一直在使用python模块XlsxWriter。你知道吗
XlsxWriter允许编码遵循基本的excel约定-我是python的新手,第一次尝试就很容易建立、运行和工作。你知道吗
以下是您的代码的更新版本,它将从该页获取所有https HREF:
如果要获取以https以外的内容开头的HREF,请将第2行更改为最后一行:)
参考文献: https://www.tutorialspoint.com/python/string_startswith.htm
您可以使用
startswith()
:相关问题 更多 >
编程相关推荐