我对此Python脚本有疑问。我正在尝试从具有家庭字符串的列表中传递值。我已经附上了剧本。在此命令page = requests.get("https://www.google.dz/search?q=lista[url]")
中,我必须将要查找的内容放在search?q=
之后。我想搜索多个关键字,所以我做了一个清单。我不怎么在该命令中传递列表中的值...
import requests
import re
from bs4 import BeautifulSoup
lista = []
lista.append("Samsung S9")
lista.append("Samsung S8")
lista.append("Samsung Note 9")
list_scrape = []
for url in lista:
page = requests.get("https://www.google.dz/search?q=lista[url]")
soup = BeautifulSoup(page.content)
links = soup.findAll("a")
for link in soup.find_all("a",href=re.compile("(?<=/url\?q=)
(htt.*://.*)")):
list_scrape.append(re.split(":(?=http)",link["href"].replace("/url?q=","")))
print(list_scrape)
谢谢!
答案 0 :(得分:2)
使用format
for url in lista:
page = requests.get("https://www.google.dz/search?q={}".format(url))
或
page = requests.get("https://www.google.dz/search?q=%s" % url)
答案 1 :(得分:1)
尝试一下。
for url in lista:
page = requests.get("https://www.google.dz/search?q="+url)
或
page = requests.get("https://www.google.dz/search?q={}".format(url))