|
发表于 2021-10-31 19:33:04
|
显示全部楼层
- import urllib.request
- # from urllib.request import quote
- from urllib.parse import quote # 上面一句不行就试试这个,我用两个都正常工作
- from bs4 import BeautifulSoup
- cn = quote("阿里巴巴")
- url = (
- "https://www.baidu.com/s?ie=utf-8&medium=0&rtt=4&bsst=1&rsv_dl=news_t_sk&cl=2&wd=%s"
- % cn
- )
- header = {
- "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) "
- "Chrome/95.0.4638.54 Safari/537.36"
- }
- request = urllib.request.Request(url, headers=header)
- response = urllib.request.urlopen(request)
- # bs = BeautifulSoup(response, "html.parser")
- print(response.read().decode("utf-8"))
复制代码 |
|