以下是一个示例代码,用于遍历URL列表并爬取tspan元素:
import requests
from bs4 import BeautifulSoup
def crawl_urls(url_list):
for url in url_list:
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')
tspans = soup.find_all('tspan')
for tspan in tspans:
print(tspan.text)
# 示例URL列表
url_list = ['http://example.com/page1', 'http://example.com/page2', 'http://example.com/page3']
crawl_urls(url_list)
这个示例使用了requests
库来发送HTTP请求并获取URL的内容。然后使用BeautifulSoup
库来解析HTML内容。find_all
方法用于查找所有匹配指定标签名的元素,这里我们查找所有的tspan
元素。
你可以根据实际需要修改这个示例代码,例如将tspan.text
改为其他操作来处理爬取到的数据。