使用Soup和Python刮取搜索结果,Split只返回一个值而不是一个列表?
当尝试使用Soup和Python 3.0x+刮取google搜索结果时,分割后的结果只有一个值,即多个URL中的一个URL 预期的输出是找到的所有URL的列表,而不是一个,然后将使用head、sep、tail分区方法清理这些URL 它发生在这个for循环之后使用Soup和Python刮取搜索结果,Split只返回一个值而不是一个列表?,python,web-scraping,beautifulsoup,split,Python,Web Scraping,Beautifulsoup,Split,当尝试使用Soup和Python 3.0x+刮取google搜索结果时,分割后的结果只有一个值,即多个URL中的一个URL 预期的输出是找到的所有URL的列表,而不是一个,然后将使用head、sep、tail分区方法清理这些URL 它发生在这个for循环之后 for link in links: x = re.split('="/url?q="',link["href"].replace("/url?q=","")) 值链接具有来自搜索页面的所有结果,循环应该使用参数链接遍历所有链接:
for link in links:
x = re.split('="/url?q="',link["href"].replace("/url?q=",""))
值链接具有来自搜索页面的所有结果,循环应该使用参数链接遍历所有链接:
完整代码
import requests
from urllib.parse import urlparse
import re
from bs4 import BeautifulSoup
import urllib.request
srchTerm = ['64503']
class AppURLopener(urllib.request.FancyURLopener):
version = "Mozilla/5.0"
opener = AppURLopener()
parser = 'html.parser' # or 'lxml' (preferred) or 'html5lib', if installed
for term in srchTerm:
resp = opener.open("https://www.google.com/search?q=site:https://private.xx.co.bd/++" + term)
soup = BeautifulSoup(resp, parser, from_encoding=resp.info().get_param('charset'))
links = soup.find_all("a",href=re.compile("(?<=/url\?q=)(https://private.xx.co.bd/)"))
for link in links:
x = re.split('="/url?q="',link["href"].replace("/url?q=",""))
## for linka in x:
##head, sep, tail = linka.('&sa')
##print(head)
导入请求
从urllib.parse导入urlparse
进口稀土
从bs4导入BeautifulSoup
导入urllib.request
srchTerm=['64503']
类AppURLopener(urllib.request.FancyURLopener):
version=“Mozilla/5.0”
opener=AppURLopener()
解析器='html.parser'#或'lxml'(首选)或'html5lib',如果已安装
对于srchTerm中的术语:
resp=开启器开启(“https://www.google.com/search?q=site:https://private.xx.co.bd/++“+任期)
soup=BeautifulSoup(resp,解析器,from_encoding=resp.info().get_param('charset'))
links=soup.find_all(“a”,href=re.compile”(?您可以尝试在拆分x=list(re.split(…))之前添加一个数据结构。预期的输出是什么,请将其包含在帖子中。感谢您的回复,尝试了,仍然是一个结果。link[“href]”的值是多少。替换(“/url?q=”,”)
您是什么getting@EduardoSavrin值为:['https://private.xxx.co.bd/blabla/401C04134777625D49130612.aspx&sa=U&ved=2ahUKEwiZqq6CzoHqAhVCFzQIHRqDDqEQFjADegQIARAB&usg=AOvVaw1yskV6XYDuxzezGskZGGiW']
能否尝试在拆分x=list(关于拆分(…))之前添加数据结构。预期的输出是什么,请将其包含在帖子中。感谢您的回复,已尝试,仍然是一个结果。link[“href”]的值是多少。替换(“/url?q=”,”)
您是什么getting@EduardoSavrin值为:['https://private.xxx.co.bd/blabla/401C04134777625D49130612.aspx&sa=U&ved=2ahUKEwiZqq6CzoHqAhVCFzQIHRqDDqEQFjADegQIARAB&usg=AOvVaw1yskV6XYDuxzezGskZGGiW']
<a data-uch="1" href="/url?q=https://private.xx.co.bd/blalbalba/4B1041344.aspx&sa=U&ved=2ahUKEwi-pOWSv4HqAhWGJTQIHUI-BCgQFjACegQIAxAB&usg=AOvVaw3joBh4SH9QwW5WHmwn-7cs"><h3 class="zBAuLc"><div class="BNeawe vvjwJb AP7Wnd"><span dir="rtl">xxxxxxx</span></div></h3><div class="BNeawe UPmit AP7Wnd"><span dir="rtl">xxx‹ https://private.xxx.co.il</span></div></a>