我有这个Python脚本的问题。我正在尝试从包含主字符串的列表中传递值。我附上了剧本。在这个命令page = requests.get("https://www.google.dz/search?q=lista[url]")
我必须在search?q=
之后把我正在寻找的东西放在google上。我想搜索多个关键字,所以我列出了一个列表。我不知道如何传递该命令列表中的值...
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)
谢谢!
使用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)
试试这个..
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))