web-scraping - 如何使用python网络抓取提取每个产品的标题
问题描述
这是链接:https ://www.118100.se/sok/foretag/?q=brf&loc=&ob=rel&p=0
def get_index_data(soup):
try:
links = soup.find_all('div','a',id=False).get('href')
except:
links = []
print(links)
解决方案
查找所有div
具有class
名称Name
(class="Name")的 。它为您提供所有标题名称。如果您愿意href
,则遍历所有内容titles
并找到a
具有.title
title.text
import requests
import bs4 as bs
url = 'https://www.118100.se/sok/foretag/?q=brf&loc=&ob=rel&p=0'
response = requests.get(url)
# print('Response:', response.status_code)
soup = bs.BeautifulSoup(response.text, 'lxml')
titles = soup.find_all('div', {'class': 'Name'})
# a = soup.find_all('a')
# print(a)
for title in titles:
link = soup.find('a', {'title': title.text}).get('href')
print('https://www.118100.se' + link)
推荐阅读
- javascript - 为什么 onerror 不拦截来自 Promise 和异步函数的异常
- php - 如何为 Wordpress 制作简单的机器人记录器?
- java - Redirect servlet to jsp
- angular - 加载资源失败:net::ERR_CACHE_WRITE_FAILURE
- html - 在 Laravel 中添加 Html 标签
- javascript - 使用 JavaScript,如何从句子 X 中获取 Y 字母计数?
- azure-cosmosdb - 如何对 Cosmos DB 中的大量记录进行快速(呃)聚合?
- vb.net - 对用户输入值的数组进行排序
- python-3.x - python ctypes回调可能被忽略?
- javascript - 关闭和刷新事件触发时跳转到注销页面