簡體   English   中英

如何從url字符串中的列表傳遞值

[英]How to pass the value from a list in a url string

我有這個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)

謝謝!

使用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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM