私はPythonのBeautifulSoupを使ってGoogleニュースのリンクをリストにしています。ここで私はこれまで得たものです:Googleニュースのリンク一覧を取得する
import requests
from bs4 import BeautifulSoup
import re
#url is just some google link, not to worried about being able to search from Python code
url = "https://www.google.com.mx/search?biw=1526&bih=778&tbm=nws&q=amazon&oq=amazon&gs_l=serp.3..0l10.1377.2289.0.2359.7.7.0.0.0.0.116.508.5j1.6.0....0...1.1.64.serp..1.5.430.0.19SoRsczxCA"
#this part of the code avoids error 403, we need to identify ourselves
browser = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7'
headers={'User-Agent':browser,}
#getting our html
page = requests.get(url)
soup = BeautifulSoup(page.content, "lxml")
#looking for links and adding them up as a list
links = soup.findAll("a")
for link in soup.find_all("a",href=re.compile("(?<=/url\?q=)(htt.*://.*)")):
list=(re.split(":(?=http)",link["href"].replace("/url?q=","")))
print(list)
私の質問は:なぜ、いくつかのリンクは動作しませんか?例えば:
Forbes El Financiero El Mundo Cnet