简体   繁体   English

我正在尝试使用python3为我的wordpress网站创建一个搜寻器

[英]I'm trying to create a crawler for my wordpress website using python3

import requests
from bs4 import BeautifulSoup

def page(current_page):
    current = "h2"
    while current == current_page:
        url = 'https://vishrantkhanna.com/?s=' + str(current)
        source_code = requests.get(url)
        plain_text = source_code.txt
        soup = BeautifulSoup(plain_text)
        for link in soup.findAll('h2', {'class': 'entry-title'}):
            href = "https://vishrantkhanna.com/" + link.get('href')
            title = link.string
            print(href)
            print(title)

page("h2")

I'm trying to copy and print the article title and the href link associated with it. 我正在尝试复制和打印文章标题以及与其关联的href链接。

You need to extract the <a> tag from the heading: 您需要从标题中提取<a>标记:

import requests
from bs4 import BeautifulSoup

URL = 'https://vishrantkhanna.com/?s=1'

html = requests.get(URL).text
bs = BeautifulSoup(html, 'html.parser')
for link in bs.find_all('h2', {'class': 'entry-title'}):
    a = link.find('a', href=True)
    href = "https://vishrantkhanna.com/" + a.get('href')
    title = link.string
    print(href)
    print(title)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我正在尝试以一种奇怪的方式使用python创建一个网站 - I'm trying to create a website with python, in an odd way 尝试使用 Python3 登录网站 - Trying to login to a website using Python3 尝试创建python Web搜寻器 - Trying to create a python web crawler 尝试创建一个简单的python Web搜寻器 - Trying to create a simple python web crawler 我正在尝试使用 Scrapy 从网站上抓取数据。我的代码有什么问题? - I'm trying to scrape data from a website using Scrapy. What's wrong with my code? 我正在尝试使用 python 动态选择图像并在网站上使用它,有没有办法做到这一点? - I'm trying to dynamically select using python an image and use it on a website, is there a way for this to work? 我只想从网站中获取一些 api 到我的代码中。 我正在使用蟒蛇 - I'd like to only source SOME of the api from a website into my code. I'm using python 在我的网站中使用我的Python Web爬网程序 - Using my Python Web Crawler in my site 我正在尝试使worldcheck网站自动化。 我无法通过Selenium IDE填充搜索框。 对于脚本,我正在使用Python - I'm trying to automate the worldcheck website. I'm unable to fill the search box through selenium IDE. For Scripting i'm using Python 我正在尝试为twitter创建一个python gui,它将把我的文本框作为tweet提交 - I'm trying to create a python gui for twitter that will submit my textbox as a tweet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM