from collections import Counter
import requests
from bs4 import BeautifulSoup
from urllib import request
import urllib
url = "http://www.baidu.com.cn/s?wd=" + urllib.parse.quote("BeautifulSoup") + "&pn=" -sharp wordpn..
headers = {
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.99 Safari/537.36",
"Connection": "keep-alive",
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8"}
r=requests.get(url,headers=headers)
soup = BeautifulSoup(r.text.replace("<b>", "").replace("</b>", ""),"lxml")
print(soup.find(id="3").find("a", "c-showurl").string)
-sharpfor i in range(1, 11):
-sharp print(soup.find(id=i).find("a", "c-showurl").string)
the problem is mainly in the last three lines. If find (id= "3") is fine, find (id=i) will report an error. How to solve it?
there is another question, that is, why the search results obtained through python are different from those found by computers?