简体   繁体   中英

How to pass the value from a list in a url string

I have a problem with this Python script. I'm attempting to pass the values from a list that has home strings in it. I've attached the script. In this command page = requests.get("https://www.google.dz/search?q=lista[url]") I have to put what I'm looking for on google after the search?q= . I want to search multiple keyword, so I made a list. I don't how to pass the values from the list in that command...

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)

Thank you!

Use format

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

Or

page = requests.get("https://www.google.dz/search?q=%s" % url)

try this..

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

or

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM