Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在url字符串中传递列表中的值_Python_Url_Beautifulsoup - Fatal编程技术网

Python 如何在url字符串中传递列表中的值

Python 如何在url字符串中传递列表中的值,python,url,beautifulsoup,Python,Url,Beautifulsoup,我对这个Python脚本有一个问题。我正在尝试从包含主字符串的列表中传递值。我已经附上了剧本。在这个命令中,page=requests.get(“https://www.google.dz/search?q=lista[url]”我必须在搜索?q=之后把我要找的东西放到谷歌上。我想搜索多个关键字,所以我列出了一个列表。我不知道如何在该命令中传递列表中的值 import requests import re from bs4 import BeautifulSoup lista = [] lis

我对这个Python脚本有一个问题。我正在尝试从包含主字符串的列表中传递值。我已经附上了剧本。在这个命令中,
page=requests.get(“https://www.google.dz/search?q=lista[url]”
我必须在
搜索?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)
导入请求
进口稀土
从bs4导入BeautifulSoup
lista=[]
附加(“三星S9”)
附加(“三星S8”)
lista.append(“三星注释9”)
列表_scrap=[]
对于lista中的url:
页面=请求。获取(“https://www.google.dz/search?q=lista[网址]”)
汤=美汤(第页内容)
links=soup.findAll(“a”)

对于soup.find_all(“a”,href=re.compile((?Use
format

for url in lista:
    page = requests.get("https://www.google.dz/search?q={}".format(url))

试试这个

for url in lista:
    page = requests.get("https://www.google.dz/search?q="+url)


可能的重复不太清楚您请求的是什么,假设
请求
url是一些开发/生产框,而不是google,这样做可能会导致灾难如果您在
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))