选择href linkwithpython请求

2024-03-29 08:22:06 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用python中的请求库在stack overflow中搜索搜索栏中的问题,然后获取前3个链接,获取页面的内容并发送到我的ConferenceAPI,但我一直在想我是如何获取python中链接指向我的html的

import requests
from bs4 import BeautifulSoup

#buscas online
def stackoverflow(question):
    questionAdjusted = question.replace(' ','+')
    Req = requests.get("https://pt.stackoverflow.com/search?q="+questionAdjusted)
    soup = BeautifulSoup(Req.text,"html.parser")
    questions = soup.select(".question-summary")

for que in questions:
    #print(que.select_one('.question-hyperlink').getText().replace('P: ',''))
    #print((que.select_one('.question-hyperlink').getText().replace('P: ', '').replace(' ','-').replace('--------','')))
    for link in soup.findAll('a', href=(que.select_one('.question-hyperlink').getText().replace('P: ', '').replace(' ','-').replace('--------',''))):
        print(link['href'])


stackoverflow('python database')

我只是做了这个直到现在


1条回答
网友
1楼 · 发布于 2024-03-29 08:22:06

要从URL获取前3个链接及其描述,可以使用下一个示例:

import requests
from bs4 import BeautifulSoup


def stackoverflow(question):
    url = "https://pt.stackoverflow.com/search"
    r = requests.get(url, params={"q": question})
    soup = BeautifulSoup(r.content, "html.parser")
    questions = soup.select(".question-hyperlink")

    for q in questions[:3]:  # <  select ony first 3
        print(q.get_text(strip=True).replace("P: ", ""))
        print("https://pt.stackoverflow.com" + q["href"])
        print()


stackoverflow("python database")

印刷品:

Select from e insert into em outro database com Python
https://pt.stackoverflow.com/questions/376648/select-from-e-insert-into-em-outro-database-com-python?r=SearchResults

Finalizando um projeto em python [duplicada]
https://pt.stackoverflow.com/questions/259591/finalizando-um-projeto-em-python?r=SearchResults

Erro de conexão com SQL Server 2012 com Python
https://pt.stackoverflow.com/questions/478779/erro-de-conex%c3%a3o-com-sql-server-2012-com-python?r=SearchResults

相关问题 更多 >