我正在使用 BeautifulSoup 从网站检索一些信息。我使用 for 循环来获取该网站的所有满足条件的元素。
但是,当我在 for 循环外调用该函数时,我只得到 for 循环检索到的最后一项。
我知道我可以在循环外创建一个空列表并附加所有项目。问题在于我使用 PyMySQL 将此信息插入 MySQL,而 MySQL 不接受列表。
我知道我也可以使用 " ".join(list) 将列表转换为字符串 问题在于 MySQL 获取这个新对象并将其发送到数据库中的一个单元格。
def getRestaurantLink(bs):
listLinks = []
restGrid = bs.find_all ("ul", {"class": "lemon--ul__373c0__1_cxs
undefined list__373c0__2G8oH"})
for i in restGrid:
link = i.find_all("a", {"href": re.compile ("/biz/.*")})
for l in link:
if "target" in l.attrs and "name" in l.attrs and "rel" in
l.attrs:
listLinks.append (l.attrs["href"])
for link in listLinks[0::3]:
links = ("https://www.yelp.com"+str(link))
print (links)
html_page = requests.get("https://www.yelp.com/search?
cflt=restaurants&find_loc=Hunts+Point%2C+Bronx%2C+NY+10474")
bs = BeautifulSoup (html_page.text, "html.parser")
getRestaurantLink(bs)
不知道加载位,但这将返回正确数量的链接作为列表,您可以稍后循环。
import requests
from bs4 import BeautifulSoup as bs
def get_restaurant_links(url):
base = 'https://www.yelp.com'
r = requests.get(url)
soup = bs(r.content, 'lxml')
links = [base + i['href'] for i in soup.select('h3 [href^="/biz"]')]
return links
some_list = get_restaurant_links('https://www.yelp.com/search?%20cflt=restaurants&find_loc=Hunts+Point%2C+Bronx%2C+NY+10474')
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句