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

2024-04-26 11:35:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我对这个Python脚本有一个问题。我试图从一个包含主字符串的列表中传递值。我附上了剧本。在这个命令page = requests.get("https://www.google.dz/search?q=lista[url]")中,我必须将我在google上寻找的内容放在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)

谢谢你!在


Tags: import命令reurl列表searchgooglepage
2条回答

试试这个。。在

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

或者

^{pr2}$

使用format

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

或者

^{pr2}$

相关问题 更多 >