编写一个可以复制百度搜索结果链接的代码
时间: 2023-06-09 09:04:04 浏览: 125
以下是 Python 代码示例,可以通过输入关键词来复制百度搜索结果的链接:
```python
import urllib.parse
import requests
from bs4 import BeautifulSoup
def baidu_search(keyword):
url = "https://www.baidu.com/s?wd=" + urllib.parse.quote(keyword)
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3'}
response = requests.get(url=url, headers=headers)
soup = BeautifulSoup(response.text, 'html.parser')
results = soup.select('.t a')
if len(results) > 0:
return results[0]['href']
else:
return None
keyword = input("请输入关键词:")
url = baidu_search(keyword)
if url:
print("搜索结果链接为:", url)
else:
print("未搜索到相关结果!")
```
运行代码后,输入关键词后即可获得对应的百度搜索结果链接。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)