S
S
stulevtoday2021-11-30 12:28:17
Python
stulevtoday, 2021-11-30 12:28:17

How to parse text from a given line?

def price_parse(links):
    import requests
    from lxml import etree

    for url in links:
        info = requests.get(url)
        tree = etree.parse(info, etree.HTMLParser())
        rarity = tree.xpath('.//*[@id="largeiteminfo_item_type"]')[0].text
        print(rarity, url)

OR
def price_parse(links):
    import requests
    from bs4 import BeautifulSoup

    for url in links:
        info = requests.get(url)
        soup = BeautifulSoup(info.content, 'html.parser')
        rarity = soup.find('div', id='largeiteminfo_item_type')
        print(rarity, url)


You need to parse the text from here:
<div id="largeiteminfo_item_type" class>Covert Pistol</div>

Covert Pistol
Sample link: https://steamcommunity.com/market/listings/730/Des...
How to do it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Kuts, 2021-11-30
@fox_12

Something like this:

import io
import requests
from lxml import etree

for url in links:
    info = requests.get(url)
    tree = etree.parse(io.StringIO(info.text), etree.HTMLParser())
    rarity = tree.xpath('.//*[@id="largeiteminfo_item_type"]')[0].text
    print(rarity, url)

PS: Read the documentation carefully to UNDERSTAND what you are doing...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question